2010-11-29 142 views

回答

2

如果您只需检查两个数字是否存在,请使用更快的strpos

if(strpos($mystring, '|7|') !== FALSE AND strpos($mystring, '|11|') !== FALSE) 
{ 
    // Found them 
} 

或者使用正则表达式慢捕捉到数

preg_match('/\|(7|11)\|/', $mystring, $match); 

使用regexpal测试正则表达式是免费的。

+0

这也将像17,27,71和111,211和114号仅举几例 – Yeroon 2010-11-29 15:22:16

+0

谢谢返回TRUE,这是一个更简单的方法和工作! – ITg 2010-11-29 15:22:51

0

如果你真的想使用preg_match(尽管我建议strpos,就像Xeoncross的回答),使用此:

if (preg_match('/\|(7|11)\|/', $string)) 
{ 
    //found 
} 
0

假设你的字符串总是启动并与|结束:

strpos($string, '|'.$number.'|')); 
17

使用\b表达式前后仅匹配它作为一个整词:

$str1 = 'foo bar';  // has matches (foo, bar) 
$str2 = 'barman foobar'; // no matches 

$test1 = preg_match('/\b(foo|bar)\b/', $str1); 
$test2 = preg_match('/\b(foo|bar)\b/', $str2); 

var_dump($test1); // 1 
var_dump($test2); // 0 

所以,在你的榜样,那就是:

$str1 = '|1|77|111|'; // has matches (1) 
$str2 = '|01|77|111|'; // no matches 

$test1 = preg_match('/\b(1|7|11)\b/', $str1); 
$test2 = preg_match('/\b(1|7|11)\b/', $str2); 

var_dump($test1); // 1 
var_dump($test2); // 0