Regular Expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roger Price

    Regular Expressions

    Well it didn't take long to come up with another query!

    The varable $HTTP_ENV_VARS['HTTP_USER_AGEN T']
    returns on my browser:

    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)'

    I have to write a PHP script that check to see if it contains MISE and
    confirm whether the browser being used is Internet Explorer and what
    version.

    The first part is easy.

    I now need to isolate "6.0" from the string. I have tried many combinations
    of "regexp" but none have come even close to finding the string of
    characters that start at the beginning and end with MSIE, followed by a
    space, thus:

    "'Mozilla/4.0 (compatible; MSIE "

    The code I am trying to use is:

    $search = $HTTP_ENV_VARS['HTTP_USER_AGEN T'];

    if ( ereg( "^([a-zA-Z0-9\(\)\.\/\;\'\s])*4.0[[:>:]]", $search,$match ) ) {
    print( "String found ending in 'IE': " .$match[ 1 ] . "<br />" );
    }


    I would then need to replace this with "" and select all characters up to
    but not including ";"

    I would welcome any advice you can offer.

    TIA

    Roger


  • Pedro Graca

    #2
    Re: Regular Expressions

    Roger Price wrote:[color=blue]
    > I now need to isolate "6.0" from the string.[/color]
    (snip)[color=blue]
    > The code I am trying to use is:
    >
    > $search = $HTTP_ENV_VARS['HTTP_USER_AGEN T'];
    >
    > if ( ereg( "^([a-zA-Z0-9\(\)\.\/\;\'\s])*4.0[[:>:]]", $search,$match ) ) {
    > print( "String found ending in 'IE': " .$match[ 1 ] . "<br />" );
    > }
    >
    >
    > I would then need to replace this with "" and select all characters up to
    > but not including ";"[/color]

    Switch to preg_* functions
    They're faster amd more powerful.

    Try this:

    <?php
    $search = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)';
    $version = preg_replace('/^.+MSIE ([^;]+);.*$/', '$1', $search);
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    Working...