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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
OM2
Forum Newbie
Posts: 2
Joined: Thu Jul 24, 2008 5:49 am

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

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

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

Post 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.
OM2
Forum Newbie
Posts: 2
Joined: Thu Jul 24, 2008 5:49 am

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

Post 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.
Post Reply