Welcome to WebmasterWorld Guest from 52.91.39.106

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

please help to customise a snippet

php, code , grab, code

     
11:16 pm on Sep 15, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 25, 2007
posts: 94
votes: 0


hello,
I got a piece of code supposed to grab content from a remote page, this content is located between 2 defined html tags.
here is the code :
<?php

$config['url'] = $row_DetailRS1['CreatedUniqueID']; // url of html to grab
$config['start_tag'] = "<p>"; // where you want to start grabbing
$config['end_tag'] = "</p>"; // where you want to stop grabbing
$config['show_tags'] = 0; // do you want the tags to be shown when you show the html? 1 = yes, 0 = no

class grabber
{
var $error = '';
var $html = '';

function grabhtml( $url, $start, $end )
{
$file = file_get_contents( $url );

if( $file )
{
if( preg_match_all( "#$start(.*?)$end#s", $file, $match ) )
{
$this->html = $match;
}
else
{
$this->error = "Tags cannot be found.";
}
}
else
{
$this->error = "Site cannot be found!";
}
}

function strip( $html, $show, $start, $end )
{
if( !$show )
{
$html = str_replace( $start, "", $html );
$html = str_replace( $end, "", $html );

return $html;
}
else
{
return $html;
}
}
}

$grab = new grabber;
$grab->grabhtml( $config['url'], $config['start_tag'], $config['end_tag'] );

echo $grab->error;

foreach( $grab->html[0] as $html )
{
echo htmlspecialchars( $grab->strip( $html, $config['show_tags'], $config['start_tag'], $config['end_tag'] ) ) . "<br>";
}

?>

Actually this code grabs the content from the page i am intterested in. However i don't want to take all the content from thi page, only the small intro...the small intro is located between the following html tag :
<p class="frist">intro text</p>
The problem here is the class of the <p> tag is not taken into account in this code.
Thanks for any suggestion
Yaz

11:24 pm on Sept 15, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


It looks like you just need to change this line:

$config['start_tag'] = "<p>"; // where you want to start grabbing

to

$config['start_tag'] = '<p class="first">'; // where you want to start grabbing

8:24 am on Sept 16, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 25, 2007
posts: 94
votes: 0


nope i have tried this before posting but i got the the error message :
Tags cannot be found.
Warning: Invalid argument supplied for foreach() in
Perhaps i need to insert a new variable containing class or style of the tag to be isolated
Thanks for any suggestion
10:19 am on Sept 16, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


That works when I test it... Make sure you've got the spelling, spaces characters etc. the same as it would appear in the page you're opening.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members