Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Grabbing content

easy question



4:38 am on Dec 3, 2003 (gmt 0)

10+ Year Member

This is a really basic question that I'm sure has been asked countless times in this forum. Someone I know taged part of his web page with tags similar to
<!-- begin content -->
<!-- end content -->
how would i use php to grab only this section of html into another file? I'm sure this has been asked before, so a link to the thread would be greatly appreciated.

i tried [webmasterworld.com ] but it didn't work


8:19 pm on Dec 3, 2003 (gmt 0)

10+ Year Member


Use something like:

$handle = fopen ("http://www.yourdomainname.biz/file.html", "r");

do {
$data = fread($handle, 8192);
if (strlen($data) == 0) {
$contents .= $data;
} while(true);
fclose ($handle);

if(ereg("<!-- begin content -->(.*)<!-- end content -->", $contents, $out)){
echo $out[1];
else {
echo "No Match";


11:40 pm on Dec 3, 2003 (gmt 0)

10+ Year Member

this works great for sites that are on my server, but i need to grab content off another site

the site i'm grabbing content from only accepts urls like

but with no file extentions


12:20 am on Dec 4, 2003 (gmt 0)

10+ Year Member

Try a Google search for 'php curl'. I was advised at Pubcon that the Curl library is the business for this kind of activity.


12:32 am on Dec 4, 2003 (gmt 0)

10+ Year Member

i don't think i can add php libraries


2:30 am on Dec 4, 2003 (gmt 0)

10+ Year Member

i asked my server support and they said that my server had curl on it. how would i use it in this situation? i don't really understand the php.net explaination

brotherhood of LAN

2:59 am on Dec 4, 2003 (gmt 0)

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

whos, as per your sticky...this should work, only prob i can see is if there's no <body> tag in the document it there wont be a $page[1] value.

// Change these variables to suit
$pathtocurl = "curl";
$pageyouwanttograb = "http://www.yahoo.com";
$filetowriteto = "writetothisfile.txt";

exec("$pathtocurl $pageyouwanttograb",$page);
echo 'couldnt get page';
$page = preg_split("'<body[^>]+>'ims",implode("",$page));
$page = $page[1];
$fp = fopen($filetowriteto,"w");

It will output "couldnt get page" if you have the wrong path to curl or the url you requested couldnt be reached.


4:42 am on Dec 4, 2003 (gmt 0)

10+ Year Member

thanks bro...works like a charm

Featured Threads

Hot Threads This Week

Hot Threads This Month