Jump to content

[SOLVED] error handling


phoenixx

Recommended Posts

I somehow need to generate a break if the code generates an error.  Any suggestions (w/ code) would be amazingly appreciated.

 

Here's the loop I need to use the error handling in:

 

$data = @file_get_contents("http://$city.domain.com/$category/index$growloop.html");
preg_match_all('/<p>.*?<a href="([^"]*)".*?<p>.*?href="([^"]*)"/is',$data,$out);
$d = array_combine($out[1], $out[2]);
foreach($d as $k=>$v){

echo $k . " - " . $v . "<br>";
}

Link to comment
https://forums.phpfreaks.com/topic/126297-solved-error-handling/
Share on other sites

once the page I'm scraping runs out of data the following error is printed to the screen:

Warning: array_combine() [function.array-combine]: Both parameters should have at least 1 element in /home/xxxxxxxx/public_html/sandbox/extractormod.php on line 49

 

Warning: Invalid argument supplied for foreach() in /home/xxxxxx/public_html/sandbox/extractormod.php on line 50

 

I would like it to just suppress the error and apply a break.

$data = @file_get_contents("http://$city.domain.com/$category/index$growloop.html");
preg_match_all('/<p>.*?<a href="([^"]*)".*?<p>.*?href="([^"]*)"/is',$data,$out);
if(is_array($out[1]) && is_array($out[2])){
     $d = array_combine($out[1], $out[2]);
     foreach($d as $k=>$v){
          echo $k . " - " . $v . "<br>";
     }
}

Don't use the @ suppressor. Here's what I liek to do when dealing with large blocks that I want to abort on error

 

<pre><?php

error_reporting( 0 );

try {

$file = "http://$city.domain.com/$category/index$growloop.html";
if( ($data = file_get_contents($file)) === FALSE )
	throw new Exception( "Unable to open $file for inclusion" );

preg_match_all('/<p>.*?<a href="([^"]*)".*?<p>.*?href="([^"]*)"/is',$data,$out);
if ( empty($out) )
	throw new Exception( 'No matches were found by regular expression' );

$d = array_combine($out[1], $out[2]);
foreach($d as $k=>$v)
	echo $k . " - " . $v . "<br>";

} catch ( Exception $e ) {
echo 'Exception was caught: ' . $e->getMessage();
}

?></pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.