Jump to content

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.

Uhm...

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

?

 

Instead of suppressing the error, why not try to prevent it in the first place?

$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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.