Get current loaded HTML as a string in PHP? [duplicate] - php

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 5 years ago.
Is it possible, using PHP, to get the current HTML loaded into a browser, as a string in PHP?
In other words, this would be the PHP equivalent of document.documentElement.outerHTML in JavaScript.
The question has probably been asked somewhere here, but I can't seem to find it. If this question has already been asked before, could you please give me link? I'm new here at StackOverflow :P

Yes, You can assign the html data using ob_start and ob_get_contents method
<?php
ob_start();
?>
<html>
<head>
<title>Test</title>
</head>
<body>
Testings
</body>
</html>
<?php
$data = ob_get_contents();
ob_end_clean();
print_r($data);
?>

Related

I want to display <!DOCTYPE html> on my site as a string [duplicate]

This question already has answers here:
How to display HTML tags as plain text [duplicate]
(11 answers)
Closed 4 years ago.
<!DOCTYPE html> is not displaying on my site, I have a simple website containing html and php question. When I render questions from database, <!DOCTYPE html> will display as it is in input or textarea but without using input field it will show nothing.
This is the question not displaying html code:
Here I have added in statement:
Use htmlspecialchars or htmlentities before outputting your data from the database.
<?php echo htmlspecialchars($string); ?>
or
<?php echo htmlentities($string); ?>
Reference
PHP Manual - htmlentities
PHP Manual - htmlspecialchars
Encode your content before outputting:
<?= htmlspecialchars($questionContent); ?>

echoing a php variable value in html html and not php file [duplicate]

This question already has answers here:
Server not parsing .html as PHP
(23 answers)
Closed 7 years ago.
building on from this question here it turns out that i have to save the file as a *.php file to display the variable value.
Is there a way to achieve this by keeping the file as a .html? But still achieving the year being displayed in the browser?
here is the code:
<?php
$year = date("Y");
echo "<p class='text-muted'>© $year. X Team</p>";
?>
So instead of haveing file.php with the above code I would have file.html with the above code but achieving the same thing.
Don't use php use Javascript.
<script language="JavaScript" type="text/javascript">
now = new Date
theYear=now.getYear()
if (theYear < 1900)
theYear=theYear+1900
document.write(theYear)
</script>
All html files should allow you to use java script.

Code PHP passed to HTML through append doesn't work [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I am trying pass this code for html but not work, load as text.
HTML:
<div id="captcha-wrap">
<p>
</p>
<!-- show the captcha result - fail/pass -->
<div id="captcha-status">
</div>
JS:
$("#captcha-wrap").append("<?php require_once('_control/showcaptcha.php'); ?>");
You can't append PHP like that. Have a look at using AJAX.

Use file_get_contents() to read from a specific div? [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
DOMDocument for parsing HTML (instead of regex)
(2 answers)
Closed 9 years ago.
I want to get the text from a div in this page:
<html>
<head>Hello world!</head>
<body>
This is a test!<br>Hello man!<br>
<div class="special">
I want this text
</div>
</body>
</html>
I am using this code to get the content without any tags:
echo strip_tags(file_get_contents('http://website.com'));
However, I would like only to get the content from the
<div class="special">
from that page. Is that possible in PHP?
Use a HTML parser to parse the object you read using file_get_contents(). This question lists a bunch of parsers you could use

getting the source code of a website dynamically with php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best methods to parse HTML with PHP
I wish to get the code of a website as you can with "view source" but dynamically.
I found a site which does exactly what i want - http://www.iwebtool.com but I wish
to implement it by myself.
any ideas? thanks!
You chould give this a try in a php page:
<html>
<head><title>title</title></head>
<body>
<xmp>
<?php echo file_get_contents('http://www.google.com'); ?>
</xmp>
</body>
</html>
You could replace 'http://www.google.com' with the value of a variable you can set dynamically.
<xmp> is deprecated also you should take care of html special chars so I'd go with:
<pre>
<?php echo htmlentities( file_get_contents( 'http://www.somewebsite.com/somepage.html' ) ); ?>
</pre>

Categories