PHP code to get the HTML code of a webpage [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I get the HTML code of a web page in PHP?
Is there a PHP code to get the HTML code of a webpage?
$html = file_get_contents('https://stackoverflow.com/questions/ask');
this doesnt work. It simply loads the webpage. I want to display the HTML code when I run the code.
Thanks in advance.

Try:
echo htmlentities(file_get_contents('http://stackoverflow.com/questions/ask'));

Just HTML encode your output, so that & becomes &, using the htmlentities function.

Perhaps try:
$html = htmlspecialchars(file_get_contents('http://stackoverflow.com/questions/ask'));
and try using the tags to output it.

$handle = #fopen("'http://www.webmasterworld.com", "rt");
$code = fread($handle,9000);
This is copied form another thread, but it should be good enough. It will get the code in $handle and then $code would contain 9000 bytes of that code.

Try using exec and wget together:
<?php
exec('wget http://stackoverflow.com/questions/ask -O', $array);
echo implode('<br />', $array);
?>

Related

PHP file to string [duplicate]

This question already has answers here:
How can I read a php file into a string using php?
(2 answers)
Closed 7 years ago.
Basically I want to run a PHP file from another PHP file to get the output as a string. I also want to be able to pass variables into it.
For example:
$result = get_output('./otherfile.php', $vars);
I have no idea how to do it. any help would be really appreciated.
Try reading this and file_get_contents
$contents = file_get_contents('http://www.stackoverflow.com/');
echo $contents;
You have two files, 'included.php' and 'launch.php':
included.php
<?php echo $hello;
launch.php
$hello = 'hi';
ob_start();
include('included.php');
$returned = ob_get_contents();
ob_end_clean();
In '$returned' you have stored the results of the 'included.php' code
(try to launch var_dump($returned));
As you can see, you can pass variables creating them before the inclusion of included.php. With ob_start you start to save the output of the page and then, with ob_get_contents, you put the result in a variable.

How to find css link from html page [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have php regex to find tag and extract css address from html page
'/<link.*?href\s*=\s*["\']([^"\']+)[^>]*>.*?\/>/i'
but it doesn't work good.can you help me to modify this code?
Perhaps
'/<link .*?(href=[\'|"](.*)?[\'|"]|\/?\>)/i'
Then you can acces the link with $2
Not that this is better than the other answer, however just in case you want to see it, I've altered your regex such that it should work as intended:
'/<link.*?href\s*=\s*["\']([^"\']+?)[\'"]/i'
Regex to find hrefs of all stylesheets can be a tricky task. You should consider using some PHP HTML parser to get this information.
You can read this article to get more information and then try this code.
// Retrieve all links and print their HREFs
foreach($html->find('link') as $e)
echo $e->href . '<br>';
// Retrieve all script tags and print their SRCs
foreach($html->find('script') as $e)
echo $e->src . '<br>';
PS: Remember, your script tag may not contain a src then it will print empty string.

PHP Variable as HTML Link with PHP Function as HREF [duplicate]

This question already has answers here:
How do I run PHP code when a user clicks on a link?
(10 answers)
Closed 9 years ago.
Here is the code:
$vote_up = 'Vote Up';
I am simply trying to have my PHP function insert a link that has a line of PHP as the href. But it does not work right. How can I do this?
Quick update: I got some good answers, however I'd like for the function to fire when clicked (hence the need for the <?php tags).
You're trying to use PHP tags inside the string context. They have no meaning and hence won't produce the output you need.
Simply use string concatenation:
$vote_up = 'Vote Up';
Or use sprintf() (bit more cleaner):
$vote_up = sprintf('Vote Up', amc_comment_vote("up"));
this is also a possibility:
$vote_up = "<a href='".amc_comment_vote("up")."'>Vote Up</a>";

Render a PHP code using jQuery [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
I got this problem rendering or making a PHP code functional using jQuery. huhu.
Can someone help me?
Here's the code I wanted to do:
document.getElementById("fade").innerHTML = '<?php echo 'SOME-PHP-CODE-HERE'; ?>
Try using
document.getElementById("fade").innerHTML = '<?php echo $var; ?>';
Check if $var has no quotes. If it has, you need to escape them using addSlashes()
And if you are using JQuery you can better write
$('#fade').html('<?php echo $var; ?>');
Try:
document.getElementById("fade").innerHTML = '<?=$php-variable;?>
Make sure it is embedded javascript and not external. (Between <script></script> tags in the .php file)
It is hard to tell, what you are asking for. But assuming you want to display some PHP code within some element and you want to do this from JavaScript, this is some solution for you:
Download PHP.js script and place it on your site, then include it so it is available from other JavaScript code.
Second, use htmlentities() function on the content you want to show (the string containing PHP code). This may look like the following:
var phpCode = '<?php echo \'something goes here\'; ?>';
document.getElementById('fade').innerHTML = htmlentities(phpCode);
As a proof that it works, see this jsfiddle.

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