Jump to content

[SOLVED] Undefined offset, how do I detect?


oni-kun

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/170227-solved-undefined-offset-how-do-i-detect/
Share on other sites

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!

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.