phoenixx Posted September 29, 2008 Share Posted September 29, 2008 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/126297-solved-error-handling/ Share on other sites More sharing options...
discomatt Posted September 29, 2008 Share Posted September 29, 2008 What do you mean... generates an error? Quote Link to comment https://forums.phpfreaks.com/topic/126297-solved-error-handling/#findComment-653092 Share on other sites More sharing options...
phoenixx Posted September 29, 2008 Author Share Posted September 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126297-solved-error-handling/#findComment-653096 Share on other sites More sharing options...
Daniel0 Posted September 29, 2008 Share Posted September 29, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/126297-solved-error-handling/#findComment-653101 Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 $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>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/126297-solved-error-handling/#findComment-653102 Share on other sites More sharing options...
redarrow Posted September 29, 2008 Share Posted September 29, 2008 to let you no.......... get rid of php errors use >> @ << before any php function ok....... Quote Link to comment https://forums.phpfreaks.com/topic/126297-solved-error-handling/#findComment-653104 Share on other sites More sharing options...
discomatt Posted September 29, 2008 Share Posted September 29, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/126297-solved-error-handling/#findComment-653109 Share on other sites More sharing options...
phoenixx Posted September 29, 2008 Author Share Posted September 29, 2008 Many thanks to all. It worked exactly as I hoped it would. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/126297-solved-error-handling/#findComment-653142 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.