Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: preg_match Help

  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    preg_match Help

    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.

    PHP Code:
    $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
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    238
    Thanks
    18
    Thanked 34 Times in 33 Posts
    Here is the working script. If you need I could support it with detailed description:

    Input:
    PHP Code:
    <?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:
    Code:
    5.2.6
    PHP Version: 5.2.6

  • Users who have thanked PHP6 for this post:

    rfresh (12-06-2008)

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Yes that worked great. I (and perhaps others) would like to know how your preg_match() statement works?
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    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?
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #5
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    238
    Thanks
    18
    Thanked 34 Times in 33 Posts
    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:

    PHP Code:
    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:

    PHP Code:
    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

  • Users who have thanked PHP6 for this post:

    rfresh (12-06-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •