Forum Moderators: coopster

Message Too Old, No Replies

please help to customise a snippet

php, code , grab, code

         

hanyaz

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

10+ Year Member



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

MattAU

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

10+ Year Member



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

hanyaz

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

10+ Year Member



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

MattAU

10:19 am on Sep 16, 2008 (gmt 0)

10+ Year Member



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.