RobinTibbs Posted January 17, 2007 Share Posted January 17, 2007 I am totally new to regular expressions but think the script i am working on now would benefit from them. basically i know that different browsers/OSs identify themselves differently in the user agent string, and i know the pattern in my head but dont know how to apply that to regular expressions.for instance i know that in firefox it is pretty much always the last part of the string,followed by a '/' then the version number. with IE its MSIE <version>how can i use regular expressions with this, as currently i am using some string functions (strpos etc)many thanks and hope that made sense :D Quote Link to comment Share on other sites More sharing options...
owenjh Posted January 17, 2007 Share Posted January 17, 2007 You can use preg_match to match if Firefox or MSIE is in a string like so:[CODE]$string = "Firefox /5.1";if (preg_match("/Firefox(.*?)/", $string)) { echo "True"; }[/CODE]or if you want to get the version number of firefox you can use preg_match_all:[CODE]$string = "Firefox /5.1";preg_match_all("/Firefox(.*)/", $string, $var);print_r($var);[/CODE]Hope this helps Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted January 17, 2007 Author Share Posted January 17, 2007 that looks like just what i need. i will give it a try, thanks :) Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted January 17, 2007 Author Share Posted January 17, 2007 using that code i get the following output[code]Array ( [0] => Array ( [0] => Firefox /5.1 ) [1] => Array ( [0] => /5.1 ) )[/code]maybe i'm doing something wrong hmm. while i'm here,can i get the version and name with one expression or are 2 needed? thanks again Quote Link to comment Share on other sites More sharing options...
effigy Posted January 17, 2007 Share Posted January 17, 2007 The first item is the entire match, the second the captured portions--those in parenthesis. Try something like:[tt]/^(\w+)\s+([\d.]+)/[/tt]Can you provide some examples of their identification strings? Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted January 17, 2007 Author Share Posted January 17, 2007 some examples:[b]Firefox 2.0[/b] - Mozilla/5.0 (Windows; U; Windows NT 5.2; en-GB; rv:1.8.1) Gecko/20061010 Firefox/2.0[b]Firefox 1.5.0.7[/b] - Mozilla/5.0 (OS/2; U; Warp 4.5; en-US; rv:1.8.0.7) Gecko/20060915 Firefox/1.5.0.7[b]IE 6[/b] - Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; {CB1C5177-05C4-4DFE-B687-AD6AF39ACD3E}; .NET CLR 1.1.4322)[b]IE 5.5[/b] - Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; )as you can see, the pattern of the BrowserName and version are the same. hope that helps Quote Link to comment Share on other sites More sharing options...
effigy Posted January 17, 2007 Share Posted January 17, 2007 Thanks. The expression I posted should work with these, capturing the browser name and the version. Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted January 17, 2007 Author Share Posted January 17, 2007 so do i just use the same code but use the expression you gave? Quote Link to comment Share on other sites More sharing options...
effigy Posted January 17, 2007 Share Posted January 17, 2007 Use[tt] preg_match [/tt]if you're matching a string that should only have one instance, and you only want one instance. Use[tt] preg_match_all [/tt]if you're matching a string that should have multiple instances, and you want them all. Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted January 17, 2007 Author Share Posted January 17, 2007 sorry to be a pain, but could you go through what the expression does and what the symbols mean? :) many many thanks though! Quote Link to comment Share on other sites More sharing options...
trochia Posted January 17, 2007 Share Posted January 17, 2007 http://www.google.com/search?hl=en&lr=&q=determine+browser+php Quote Link to comment Share on other sites More sharing options...
effigy Posted January 17, 2007 Share Posted January 17, 2007 See the links in my signature.^ anchors the expression to the beginning of the line (BOL).(\w+) captures 1 or more "word" (a-z, 0-9, _) characters. The parens do the actual capturing.\s+ consumes 1 or more instances of whitespace (spaces, new lines, tabs).([\d.]+) captures (again, the parens) one or more digits (\d) or periods (.). These are grouped together by a character class ([ ]).The beginning and ending forward slash delimit the expression. Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted January 18, 2007 Author Share Posted January 18, 2007 just used that expression and it seems to not be capturing any info, the output i get is:[code]Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) )[/code]trying to figure out what i'm doing wrong *goes off and tinkers* Quote Link to comment Share on other sites More sharing options...
effigy Posted January 18, 2007 Share Posted January 18, 2007 [code]<pre><?php $tests = array( 'Firefox 2.0 - Mozilla/5.0 (Windows; U; Windows NT 5.2; en-GB; rv:1.8.1) Gecko/20061010 Firefox/2.0', 'Firefox 1.5.0.7 - Mozilla/5.0 (OS/2; U; Warp 4.5; en-US; rv:1.8.0.7) Gecko/20060915 Firefox/1.5.0.7', 'IE 6 - Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; {CB1C5177-05C4-4DFE-B687-AD6AF39ACD3E}; .NET CLR 1.1.4322)', 'IE 5.5 - Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; )' ); foreach ($tests as $test) { preg_match('/^(\w+)\s+([\d.]+)/', $test, $matches); print_r($matches); }?></pre>[/code] Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted January 18, 2007 Author Share Posted January 18, 2007 i think perhaps there was a misunderstanding?the bit preceding the - in the ID strings isnt actually part of the string, just was used as pretty formatting by me. sorry if that caused confussion Quote Link to comment Share on other sites More sharing options...
effigy Posted January 19, 2007 Share Posted January 19, 2007 [code]<pre><?php $tests = array( ### Firefox 2.0 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-GB; rv:1.8.1) Gecko/20061010 Firefox/2.0', ### Firefox 1.5.0.7 'Mozilla/5.0 (OS/2; U; Warp 4.5; en-US; rv:1.8.0.7) Gecko/20060915 Firefox/1.5.0.7', ### IE 6 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; {CB1C5177-05C4-4DFE-B687-AD6AF39ACD3E}; .NET CLR 1.1.4322)', ### IE 5.5 'Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; )' ); foreach ($tests as $test) { preg_match('%(MSIE|Firefox)[ /]([\d.]+)%', $test, $matches); print_r($matches); }?></pre>[/code] Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted January 19, 2007 Author Share Posted January 19, 2007 thank you very much, that works perfectly, and goes along way to help me cracking regular expressions. Quote Link to comment 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.