本帖最后由 default7 于 2014-10-27 09:30:18 编辑 使用Sublime或者很多编辑软件的搜索功能都会有这个功能,完整匹配单词,这个是如何实现的?
/**
* @param $str
*
* @return string
*/
function getValue($str)
{
return preg_match('/(?:\D)(\d{8})(?:\D)/', $str, $matches) ? $matches[1] : '';
}
$arrStr = [
'10208899', //有
'#10208899', //有
'#test^10208899', //有
'#ff1020889900',//无
'#0010208899',//无
'#1020^10208899a',//有
'#5566880&10208899f',//有
'test?#10208899',//有
'#10208899.'//有
];
foreach ($arrStr as $i => $str) {
$value = getValue($str);
echo "$i\t{$str}\t\t=>\t{$value}\n";
}
以上匹配是错误的,0、1、2、7这些没有匹配出来。
010208899=>
1#10208899=>
2#test^10208899=>
3#ff1020889900=>
4#0010208899=>
5#1020^10208899a=>10208899
6#5566880&10208899f=>10208899
7test?#10208899=>
8#10208899.=>10208899
------解决思路----------------------
function getValue($str)
{
return preg_match('/(?:^
------解决思路----------------------
\D)(\d{8})(?:\D
------解决思路----------------------
$)/', $str, $matches) ? $matches[1] : '';
}010208899=>10208899
1#10208899=>10208899
2#test^10208899=>10208899
3#ff1020889900=>
4#0010208899=>
5#1020^10208899a=>10208899
6#5566880&10208899f=>10208899
7test?#10208899=>10208899
8#10208899.=>10208899
相关文章
相关视频