...

View Full Version : preg_match Help



rfresh
12-06-2008, 03:25 PM
I'm trying to fetch the version number of PHP from a text stream. This is just a test for a structure I plan to use to fetch other data. The fact that I am fetching the PHP version out of a text file isn't the end goal but the way I'm trying to do it is. I'm using regex. My code snippet below only displays "PHP Version:" and not "PHP Version: 5.2.6".

My preg_match() function is working correctly to pull the '5.2.6' value into my var $out3.



$view = "PHP version 5.2.6";
preg_match('/PHP version(.*)/smiU', $view, $out3);
echo $out3[1].'<br>';
$phpVer = trim($out3[1]);
echo 'PHP Version: '.$phpVer;

PHP6
12-06-2008, 03:45 PM
Here is the working script. If you need I could support it with detailed description:

Input:

<?php
$view = "PHP version 5.2.6";
preg_match('#PHP\s*version\s*([\d\.]*)#', $view, $out3);
echo $out3[1].'<br>';
$phpVer = trim($out3[1]);
echo 'PHP Version: '.$phpVer;
?>

Output:

5.2.6
PHP Version: 5.2.6

rfresh
12-06-2008, 05:22 PM
Yes that worked great. I (and perhaps others) would like to know how your preg_match() statement works?

rfresh
12-06-2008, 05:29 PM
I see looking closer at your preg_match() that it is detecting only digits to get the '5.2.6'.

What about detecting *any* kind of alpha numeric data?

PHP6
12-06-2008, 07:42 PM
You are welcome ;)

As for the regular expression, of cause we could rewrite it to match any char. In that case we must to have some other sign to identify when our information starts and when it finishes.

First example: let's say that our information is up to the first white space char

or

Second example: let's say up to the end of the line.

First example's solution: in that case we will have such regular expressions:


preg_match('#PHP\s*version\s*([^\s]*)#', $view, $out3);

NOTICE: I would like to mention that it is good idea to use \s instead of simple spaces, since it will match any white space char (including tab).

The [] is used descript the set of chars we want to match. We need all chars until the first white space, so I have added ^ negative sign which means: do not select white space char (or select everything except white space). The * indicates that we need as much chars to select as it is possible, so it will select everything until the first white space.

Second example's solution: here is the second regular expression:


preg_match('#^PHP\s*version\s*(.*?)$#m', $view, $out3);

in that example we have added M modifier which will force engine to treat the whole text not as a single line (with one beginning and end of the line) but as a multi line text. Second we have added ^ and $ chars, they indicate the beginning and end of the line accordingly. In other words we said that we need following text:

[started line]PHP[any amount of white spaces]version[any amount of white spaces][as few char as possilbe][end of the line]

the ? is necessary to avoid the situation when we the engine will select from the first beginning of the line and till the end of the last line. It turns off greediness of "." char.

I hope you got it, but case you have some other question please do not hesitate to ask ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum