473,466 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
1 1649

// 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" <Be******@dickgiesser.net> wrote in message
news:c4**********@uns-a.ucl.ac.uk...
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

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
6
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
0
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.