Forum Moderators: coopster

Message Too Old, No Replies

hiding a specific error

error handling, php, hide

         

hanyaz

9:56 am on Sep 20, 2008 (gmt 0)

10+ Year Member



Hello,
I got a page where sometimes the following error appears on the browser :
Warning: Invalid argument supplied for foreach()

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>";
}

?>

barns101

1:50 pm on Sep 20, 2008 (gmt 0)

10+ Year Member



Adding the @ character before a function suppresses errors:

@foreach( $grab->html[0] as $html )

hanyaz

2:09 pm on Sep 20, 2008 (gmt 0)

10+ Year Member



this is what i got when i do it
Parse error: syntax error, unexpected T_FOREACH

I have heard that @ tip is not applicable to all functions...
does somebody have any suggestion please ?
yaz

Nutter

6:08 pm on Sep 20, 2008 (gmt 0)

10+ Year Member



Check and make sure that $grab->html[0] is an array (is_array()) before doing the foreach.

hanyaz

1:44 am on Sep 21, 2008 (gmt 0)

10+ Year Member



Should i add

is_array($grab->grabhtml [0]);

before the foreach ?
I got no idea on where in can insert it in the above code...

PHP_Chimp

5:21 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




...
$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()';
}
?>

hanyaz

8:08 pm on Sep 21, 2008 (gmt 0)

10+ Year Member



Thank you PHP_Chimp ! That worked for me
Best regards !
Han yaz