oni-kun Posted August 14, 2009 Share Posted August 14, 2009 I have this code to get the IP address and turn it into the host, and extract 'isp.net' for example out of it.. But if there is no host, it throws the error.. "Undefined offset(0) in line xxx".. $fullhost = gethostbyaddr($ip); preg_match("/^(http:\/\/)?([^\/]+)/i",$fullhost, $matches); $host = $matches[2]; preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches); echo "<b>ISP : "; if($matches[0]==null){ echo strtoupper($matches[0]); }else{ echo "ISP : [Cannot resolve]"; } My code works, it simply throws that error when it can't resolve a host, since the preg_match doesn't pass through, how do I catch the 'offset not defined' and make it display the ELSE? Quote Link to comment Share on other sites More sharing options...
Bjom Posted August 14, 2009 Share Posted August 14, 2009 in line xxx... in which line would that be? did you place your if (isset(... right? It needs to be right after the first preg_match... Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 14, 2009 Author Share Posted August 14, 2009 in line xxx... in which line would that be? did you place your if (isset(... right? It needs to be right after the first preg_match... Notice: Undefined offset: 0 in D:\wamp\www\projects\trace.php on line 152 $fullhost = gethostbyaddr($ip); echo $fullhost; preg_match("/^(http:\/\/)?([^\/]+)/i",$fullhost, $matches); if(isset($matches[0])){ $host = $matches[2]; preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches); echo "<b>ISP : "; echo strtoupper($matches[0]); //The error line 152 }else{ echo "ISP : [Cannot resolve]"; } It still displays undefined offset, I don't know if isset() is the thing to use to catch this.. help! Quote Link to comment Share on other sites More sharing options...
Bjom Posted August 14, 2009 Share Posted August 14, 2009 if(isset($matches[0], $matches[2])){ Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 14, 2009 Author Share Posted August 14, 2009 Alright, got it to work. Thanks! Quote Link to comment 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.