移动正则表达式用户代理 [英] Mobile regexp user agent

查看:60
本文介绍了移动正则表达式用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为移动用户代理签入我的脚本.

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

我有

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

我发现lg必须是字符串的第一部分

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

例如:

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

基本上,我想知道是否有可能从头开始而不是在字符串的某处在用户代理中找到该字符串(来自我的regexp).

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.

我不想更改我的正则表达式字符串

谢谢

推荐答案

您可以使用元字符^表示字符串的开头.您可以在此处了解更多信息.以下模式仅在字符串开头时匹配lg:

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/

您可以在此处看到一个运行中的简单示例: http://regexr.com?2vjec

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

这篇关于移动正则表达式用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆