Forum Moderators: coopster
The foreach argument is applied to some text generated on the fly using the specific code below; it detects tags on a given page, and grabs the intro line of the page....however the tags corresponding to the intro text are not alway present in the target page and the error stated before appears on the browser.
I want to avoid displaying this error, if the target page does not contain any intro tags and live the space blank...I have been looking around, in the php manual with the error_reporting function but no success i put the function in the head and it is not working.
Please advice me
Thanks in advance
Yaz
<?php
$config['url'] = $row_DetailRS1['CreatedUniqueID']; // url of html to grab
$config['start_tag'] = "<p class='firstLine'>"; // 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 = 'error';
var $html = 'g';
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 = ""; //beffore was tag cannot be found
}
}
else
{
$this->error = ""; //bofore was 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( strip_tags($html), $config['show_tags'], $config['start_tag'], $config['end_tag'] ) ) . "<br>";
}
?>
...
$grab = new grabber;
$grab->grabhtml( $config['url'], $config['start_tag'], $config['end_tag'] );
echo $grab->error;
if (is_array($grab->html[0])) {
foreach( $grab->html[0] as $html )
{
echo htmlspecialchars( $grab->strip( strip_tags($html), $config['show_tags'], $config['start_tag'], $config['end_tag'] ) ) . "<br>";
}
}
else {
echo '$grab->html[0] is not an array, so you cant use foreach()';
}
?>