yakoup46 Posted April 18, 2009 Share Posted April 18, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/154688-need-help-what-does-this-stuff-mean/ Share on other sites More sharing options...
yakoup46 Posted April 18, 2009 Author Share Posted April 18, 2009 I am so confused. LOL Quote Link to comment https://forums.phpfreaks.com/topic/154688-need-help-what-does-this-stuff-mean/#findComment-813422 Share on other sites More sharing options...
nrg_alpha Posted April 19, 2009 Share Posted April 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154688-need-help-what-does-this-stuff-mean/#findComment-813478 Share on other sites More sharing options...
yakoup46 Posted April 19, 2009 Author Share Posted April 19, 2009 That actually helps a lot but what is the purpose of the | |. Quote Link to comment https://forums.phpfreaks.com/topic/154688-need-help-what-does-this-stuff-mean/#findComment-813805 Share on other sites More sharing options...
nrg_alpha Posted April 19, 2009 Share Posted April 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154688-need-help-what-does-this-stuff-mean/#findComment-813835 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.