简体   繁体   中英

Regex matching phone numbers (with PHP preg_replace) Part 2

So ive been using preg_replace in PHP to match and replace phone numbers. My goal is quite simple: i want to match all character sequences which contain spaces, numbers, dashes and + sign with a minimum length of 6, so a character sequence of +12 0 123 44 44 555 would match.

String length of the $subject can be up to 1000 characters, if that makes a difference.

i came up with this regex:

preg_replace('/[0-9 +-]{6,}/', ' [hidden] ', '+12 0 123 44 44 555', -1, $count); my expectation is i get a string of

[hidden] what i get is

[hidden] 44 555 Im sure its obvious but i cant seem to figure out why the whole sequence doesent match.

I tested it on https://www.functions-online.com/preg_replace.html and also tried some suggested Regexes like: [0-9\h+-]{6,} or preg_replace('/+?\d(?:[\s+()-]*\d){5,}/', ' [hidden] ', '+12 0 123 44 44 555');

but both still only replace part of the phone number.

(previous post where only part of the question was answered and the post was closed: Regex matching phone numbers (with PHP preg_replace) )

Since your string contains non-ASCII whitespace characters, you need to use

preg_replace('/[0-9\s+-]{6,}/u', ' [hidden] ', '+12 0 123 44 44 555', -1, $count);

See the PHP demo .

The regular space is replaced with a \s shorthand character class, and the u flag is used to ensure the string is handled as a Unicode string and not a byte string by the regex engine, and \s now matches any Unicode whitespace chars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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