killua0083
killua0083

Reputation: 55

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. in 6221363138994523213 , so I write this, but it return empty, can you help me write a right pattern, thanks a lot!

Upvotes: 1

Views: 93

Answers (1)

Chris
Chris

Reputation: 5876

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])

Upvotes: 1

Related Questions