Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/34615-quick-regular-expressions-tutorial/
Share on other sites

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
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
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
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.
[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]
[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]
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.