Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile regexp user agent

Tags:

regex

php

mobile

i have to check in my script for user agent for mobile.

i have

if(preg_match('/lg|iphone|blackberry|opera/i', $_SERVER['HTTP_USER_AGENT'])) { ... }

i found out the lg one needs to be the first part of the string

eg:

lg-9000 ...
lg1000 ...
lg 2000 ...
lge4300 ...

basically i want to know if it's possible to find if the string (from my regexp) can be found in the user agent starting from the beginning, not somewhere in the string.

i would like not to change my regex string

thanks

like image 435
aki Avatar asked Dec 28 '11 19:12

aki


1 Answers

You can use the meta character ^ to denote start of string. You can read more about it here. The following pattern will only match lg if it's in the start of the string:

^lg|iphone|blackberry|opera/

You can see a simple example it in action here: http://regexr.com?2vjec

like image 69
Marcus Avatar answered Oct 24 '22 21:10

Marcus