Optimise regular expression code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin Dickgiesser

    Optimise regular expression code

    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=.{2,4}\\].*\\[\\/ip\\]",$post['pagetext'], $ip);
    $ip = eregi_replace(" \\[ip=.{2,4}\\]","",$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])) {
    if(!$takenips)$ 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!
    Benjamin Dickgiesser


  • Chung Leong

    #2
    Re: Optimise regular expression code


    // convert ip address to links
    $new_text = preg_replace('/\b(\d{1,3}\.\d{ 1,3}\.\d{1,3}\. \d{1,3})\b/', '<a
    href="http://$1">$1</a>', $text);

    // see how many ip there are
    preg_match_all( '/\b(\d{1,3}\.\d{ 1,3}\.\d{1,3}\. \d{1,3})\b/', $text,
    $matches);
    $count = count($matches[0]);

    "Benjamin Dickgiesser" <Benjamin@dickg iesser.net> wrote in message
    news:c40qi3$mtc $1@uns-a.ucl.ac.uk...[color=blue]
    > Hi, I'm trying to identify in a string ip's and convert these into links[/color]
    and[color=blue]
    > 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[/color]
    this[color=blue]
    > isn't optimal either..
    >
    > Here is my code
    >
    >
    > eregi("\\[ip=.{2,4}\\].*\\[\\/ip\\]",$post['pagetext'], $ip);
    > $ip = eregi_replace(" \\[ip=.{2,4}\\]","",$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])) {
    > if(!$takenips)$ 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[/color]
    would[color=blue]
    > 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[/color]
    can[color=blue]
    > handle large strings aswell?
    >
    > thx!
    > Benjamin Dickgiesser
    >
    >[/color]


    Comment

    Working...