简体   繁体   English

PHP找到匹配的eregi?

[英]PHP finding a match eregi?

I have this code here 我这里有这段代码

$search = $_SERVER['HTTP_USER_AGENT'];

The Search returns Mozilla/5.0 (Android; Tablet; rv:14.0) Gecko/14.0 Firefox/14.0 搜索返回Mozilla/5.0 (Android; Tablet; rv:14.0) Gecko/14.0 Firefox/14.0

This find is inside an array 这个发现在一个数组里面

$find = '(Nexus 7) | ((Android) && (Tablet))';

This if statement is inside a foreach statement hence the break. 该if语句位于foreach语句内,因此会中断。

if(eregi($find, $search)) {
    break;
}else{
    $os = "Unknown";
}

and my if statment returns Unknown!! 和我的如果陈述返回未知! is there something wrong with my find string ((Android) && (Tablet)) or is eregi not what I want to use? 我的查找字符串((Android)&&(Tablet))有问题吗,或者eregi不是我要使用的东西吗?

This can be accomplished using strpos() and an if() test: 这可以使用strpos()if()测试来完成:

if ( false !== strpos( $search, 'Nexus 7' ) ||
     ( false !== strpos( $search, 'Android' ) && 
       false !== strpos( $search, 'Tablet' ) ) { ... }

Note that the 'value-and-type' test ( !== ) must be used here because strpos will return '0' if the search term is found at the beginning of the target string. 请注意,此处必须使用'value-and-type'测试( !== ),因为如果在目标字符串的开头找到搜索词,则strpos将返回'0'。

if (!preg_match("/Nexus 7|Andriod|Tablet/", $_SERVER['HTTP_USER_AGENT']) $os = "unknown";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM