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","&nbsp;",$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!

    It looks like you want to restrict users by a IP. In this case its easier to use a database.

      No that's not the case. I simply need to convert a large string which contains ip's into links 😉

        and everytime if you add an IP you want to change the script?

          Hmm I don't get it, no I don't want to change the script at all.. (only now to optimise it 🙂 )

          The input string is variable it depends on user input and is being received from a database..

            Then it looks like easy to check them against the database...

              I think you don't understand my case 🙂

              It works like this.
              A user writes a message (like in a forum).
              This message is being stored into a database.
              You can read the message on another page. If the user used [ip] tags in his message all ip's between these tags should be converted into links.
              The ip's aren't fixed they can be anything..

                From the [man]ereg[/man] manual:

                "Note: preg_match(), which uses a Perl-compatible
                regular expression syntax, is often a faster
                alternative to ereg()."

                Also...

                You're splitting the string into lines, then looping
                over the lines. That probably incurs some
                overhead. Leave it all as one string, and use
                [man]preg_match_all[/man].

                In your regular expression you are using an
                unescaped dot. That matches any character.
                Better to use . instead (back-slashdot? 😉 ). Not
                only will this give more reliable matches, it may
                speed things up by allowing the regular expression
                engine to rule out parts of the string sooner (if
                they don't contain an actual dot).

                Use [man]microtime[/man] to test the effect of
                each change, to find out what really is faster.

                  Sorry, I saw this tags in the top but i thought they are wrong parsed by BB...😕

                  I can't help you with your Script (otherwise i don't have enough time) I think you can find some snippets on the net. I found this some days before:
                  http://www.3site.it/index.php?section=classes

                  i hope this will show you a way

                    Write a Reply...