Mobile Regular Expression User Agent

I need to check my script for a user agent for mobile devices.

I have

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

I recognized lg , you need to be the first part of the line

eg:

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

Basically, I want to know if it is possible to find a string (from my regular expression) in the user agent, starting from the beginning, and not somewhere in the string.

I would not want to change the regex string

thanks

+6
source share
2 answers

You can use the ^ metacharacter to indicate the beginning of a line. You can read about it here . The following pattern will only match lg if it is at the beginning of the line:

 ^lg|iphone|blackberry|opera/ 

Here you can see a simple example: http://regexr.com?2vjec

+11
source

I use this library to detect information about the OS and browsers http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

-2
source

Source: https://habr.com/ru/post/904625/


All Articles