Web Hosting Talk







View Full Version : preg_match_all woes... help!


LiNUxG0d
12-06-2005, 06:37 PM
Hey all!

Language: PHP

I have a script that I'm creating to match a bunch of regex's (in a mail message with headers). I'm REALLY stumped on this one. I'm at a point where I'm trying to match IP's and it's giving me a heck-load of grief.

I've tried using Regex Coach as a debugging tool because I can't figure this out. I pasted my text (mail headers and message), pasted my regex and boom, they're all highlighting. When I run my preg_match_all($pattern, $message, $matches) in PHP though, it returns one match only.

Code snippet:

$pattern = "/\b(?:\d{1,3}\.){3}\d{1,3}\b/";
$message = implode("", file("./testmessage"));

preg_match_all($pattern, $message, $results);

print "The size of the resulting array is: ".sizeof($results) . "\n";

for ($i=0;$i<sizeof($results);$i++) {

print $results[0][$i]."\n";
}

Results, when run in shell:

The size of the resulting array is: 1
204.92.87.244

I know the list should be much longer as it's a mail header and there are quite a few in there.

I've also tried this expression, which returned SOME of the IPs, just not ALL of them... like, it stops after 5 found however again, Regex Coach finds them all:

$pattern = "/(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])/";

Can someone help? For some reason I can never get all my IPs matched and my regex's are crapping out. Any help would be much appreciated. :(

Let me know and thank you all so much for your time.

Warmest regards,

Jacob

P.S. I am aware Regex Coach is mostly for Perl (or so I was told) but I believe PCRE would be smart enough to work my simple expressions. :(

LiNUxG0d
12-07-2005, 01:36 AM
[UPDATE]

I have found the problem!

The correct loop is:

for ($i=0;$i<count($results, 1);$i++) {

print $results[0][$i]."\n";

}

The result from a preg_match_all is an array pointing to an array with 2 elements. You need to count recursively. :P

Just thought you'd all like to know!

Jacob

Burhan
12-07-2005, 03:50 AM
You also could have used any one of these techniques, for is a bad way to loop through an array:

print_r($results);

foreach($results as $key => $value)
{
if (is_array($key))
{
print_r($value);
} else {
print_r($value);
}
}

LiNUxG0d
12-07-2005, 10:22 AM
You also could have used any one of these techniques, for is a bad way to loop through an array

I'm kind of curious.

I've been programming for years now and have never been told that a for loop would be a bad way to output an array. ;)

Let me know. I'm not debating it though and REALLY appreciate the feedback.

Thanks much for your time,

Jacob

Burhan
12-07-2005, 12:40 PM
Well, you really should use the best tool for the job, and in PHP -- foreach() is a special construct designed (and optimized) to step through arrays (and it only works on arrays with one exception noted below).

You get the following benefits "for free" with foreach():

1. Speed optimized for arrays. You are not incrementing a counter, or doing bounds checking or calling external functions as you would with while(list($key,$val) = each($array))
2. No need to implement bounds checking, as this is given for free
3. Assignment can be done in the expression itself. There are two ways to write a foreach. foreach($array as $val) -- this will assign each value to $val, and foreach($array as $key => $val) -- this will give you both the keys and the values.
4. You can also iterate over objects using foreach (only PHP 5+)

Always glad to offer an explanation :)