Page 1 of 1

How do I get the source code of a web page?

Posted: Thu Jul 24, 2008 5:56 am
by OM2
Can someone tell me how I can get the source code of a web page?

Newbie to PHP: don't know where to start.

I want a small script that you can call, giving a URL as input.
And then have the code give the whole source code as output on the browser screen.

Thanks.


OM

Re: How do I get the source code of a web page?

Posted: Thu Jul 24, 2008 6:31 am
by Oren
Depends what you "source code". If you want to see the php code - you can't.
If you mean like when you right click on the page and choose "view source", then you can use file_get_contents() and pass a link as an argument.

Re: How do I get the source code of a web page?

Posted: Thu Jul 24, 2008 7:13 am
by OM2
wow: that did the trick! yes: all i wanted was just the source code of the html. thanks.

i've got the following code now:

Code: Select all

<?php 
    $rawPageCode = htmlspecialchars(file_get_contents('http://www.domain.com')); 
    $cleanPageCode = preg_replace("/<!--\s*Admin Code Start\s*-->.*?<!--\s*Admin Code End\s*-->/is", "", $rawPageCode);
    echo $cleanPageCode;
?>
as an extra... i also needed to leave certain parts out that i didn't want to output.
thats what the second line is about.