Jump to content

Recommended Posts

<?php

$useragent = $_SERVER['HTTP_USER_AGENT'];

 

if (preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {

    $browser_version=$matched[1];

    $browser = 'IE';

} elseif (preg_match( '|Opera ([0-9].[0-9]{1,2})|',$useragent,$matched)) {

    $browser_version=$matched[1];

    $browser = 'Opera';

} elseif(preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {

        $browser_version=$matched[1];

        $browser = 'Firefox';

} elseif(preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {

        $browser_version=$matched[1];

        $browser = 'Safari';

} else {

        // browser not recognized!

    $browser_version = 0;

    $browser= 'other';

}

 

print "Browser: $browser $browser_version";

?>

 

I understand what the script does, I just need to know how any why.

I somewhat understand what is going on, but i am not sure what the [1] means. Or what all the / and +'s means next to like Firefox.

 

Basically I was wondering if anyone could explain to me what the heck is going on with this, and how it works.

Link to comment
https://forums.phpfreaks.com/topic/154688-need-help-what-does-this-stuff-mean/
Share on other sites

When a preg pattern is matched, the entire pattern (which must be all there in the variable in question) is stored into array element[0]. The brackets are captures.. so anything found within the brackets are stored into array element[1].. if there is a second pair of capturing brackets within the same pattern, that gets stored into array element[2], etc..

 

So if you look at the first example:

preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)

 

If the entire pattern is there, it is stored into $matched[0] (due to the third argument within the preg statement, $matched)... all the numbers (sandwiching a dot match all (which should be escaped if the intent is to look for a literal dot) would be stored into $matches[1].

 

The + is called a quantifier (one or more times). So something like: [0-9]+ means a digit one or more consecutive times.. On a note about the character class, the dots inside those don't need to be escaped.. thus [0-9\.]+ could be simply [0-9.]+

 

And finally, the / simply means looking for just that, a / character.

 

The above was a simplified set of explanations... You can learn about regex in the following links:

 

Resources

Regular-expressions

webtoolscollection

Mastering Regular Expressions

Regular Expressions (Part1) - Basic Syntax

PCRE - PHP Manual

Those are delimiters. In preg statements, the entire pattern has to be contained with those.

Delimiters don't have to be |...| They can be any non-whitespace, non alphanumeric ASCII character other than a backslash. I urge you to have a good read through the links provided, as you will learn a lot about regex within those.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.