Hi, I'm trying to identify in a string ip's and convert these into links and I want to know how many ip's there are. The ip's have to be between ubb tags..
i.e.
[ip] This is the ip of my pc: 192.168.1.1
of my friend: 192.168.1.2
192.168.1.3
192.168.1.4 etc..
[/ip]
The rest should stay untouched. I've done this but if the string gets too large the server does not even load the page..
At the moment it also requires every ip to be on a different line but this isn't optimal either..
Here is my code
eregi("\\[ip\\].*\\[\\/ip\\]",$post['pagetext'], $ip);
$ip = eregi_replace("\\[ip\\]","",$ip[0]);
$ip = eregi_replace("\\[\/ip\\]","",$ip);
$ips = explode ("\n", $ip);
$array_count = count($ips);
for($y=0; $y<$array_count; $y++) {
if(isset($ips[$y]) ) {
$ips[$y] = eregi_replace("^\r"," ",$ips[$y]);
$ips[$y] = eregi_replace("\r","",$ips[$y]);
if(!ereg("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}",$ips[$y], $ip)) $iplisting .= "$ips[$y]<br />";
elseif (is_ip($ip[0])) {
$iplisting .= eregi_replace($ip[0],"<a href=\"here is my link\">$ip[0]</a>", $ips[$y]."<br>");
}
}
}
And I use this function (if someone got a better function for this it would be appreciated if you'd post it here ):
function is_ip($ip) {
$valid = TRUE;
if(preg_match("/^((127)|(192)|(10).*)$/", "$ip")) {
return FALSE;
}
$ip = explode(".", $ip);
if(count($ip)!=4) {
return FALSE;
}
foreach($ip as $block) {
if(!is_numeric($block) || $block>255 || $block<0) {
$valid = FALSE;
}
}
return $valid;
}
Can someone give me a starting point how to improve this code so that it can handle large strings aswell?
thx!