preg match for Safari

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

preg match for Safari

Post by mikebr »

I am trying to get a preg_match for 'Safari', but the following doesn't seem to work, anyone see my mistake?

The input is 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412.6.2 (KHTML, like Gecko) Safari/412.2.2'

Code: Select all

if ( preg_match("/Safari ([0-9]\.[0-9]{0,2})/i", $this->get_user_agent(), $found) &&

				 strstr($this->get_user_agent(), "Mozilla") )

		{

		 	$this->browser = "Safari " . $found[1];

		}
Thanks in advance
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Code: Select all

preg_match("#Safari/([0-9]*)(\.[0-9]*){0,2}#i"....)
should be closer.
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

Cheers, seems to work fine.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

FYI: get_browser()

if you can't use it, you can build your own using a combination of parse_ini_file() and some fun with regex..
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

The last example works fine but this does look interesting, thanks for the info.
Post Reply