简体   繁体   English

PHP preg_match_all 返回空

[英]PHP preg_match_all return empty

This is my code:这是我的代码:

$str = "sdf13631389945fssx6221363138994523213af";
preg_match_all("/^1\d{10}$/",$str,$result);
var_dump($result);

I want to match the phone no.我想匹配电话号码。 13631389945 but don't want to match this phone no. 13631389945 但不想匹配这个电话号码。 in 6221363138994523213 , so I write this, but it return empty, can you help me write a right pattern, thanks a lot!在 6221363138994523213 ,所以我写了这个,但它返回空,你能帮我写一个正确的模式,非常感谢!

It doesn't return a result because the whole string doesn't match the expression.它不会返回结果,因为整个字符串与表达式不匹配。 If you only want part of the string to match then get rid of the ^ and $ .如果您只想匹配部分字符串,则去掉^$

preg_match_all("/1\d{10}/",$str,$result);

To not match the phone number in the second part of the string you are going to have to explain what exactly makes it an invalid match.要不匹配字符串第二部分中的电话号码,您将不得不解释究竟是什么使它成为无效匹配。 If it's the surrounding numbers then use this:如果是周围的数字,那么使用这个:

(?:^|[^\d])(1\d{10})(?:$|[^\d])

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

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