1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That did it thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.