简体   繁体   English

正则表达式匹配电话号码(使用 PHP preg_replace)第 2 部分

[英]Regex matching phone numbers (with PHP preg_replace) Part 2

So ive been using preg_replace in PHP to match and replace phone numbers.所以我一直在 PHP 中使用 preg_replace 来匹配和替换电话号码。 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.我的目标很简单:我想匹配所有包含空格、数字、破折号和 + 符号的字符序列,最小长度为 6,因此 +12 0 123 44 44 555 的字符序列将匹配。

String length of the $subject can be up to 1000 characters, if that makes a difference. $subject 的字符串长度最多可以有 1000 个字符,如果有区别的话。

i came up with this regex:我想出了这个正则表达式:

preg_replace('/[0-9 +-]{6,}/', ' [hidden] ', '+12 0 123 44 44 555', -1, $count); 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. [隐藏] 44 555 我确定它很明显,但我似乎无法弄清楚为什么整个序列不匹配。

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');我在https://www.functions-online.com/preg_replace.html上对其进行了测试,还尝试了一些建议的正则表达式,例如:[0-9\h+-]{6,} 或 preg_replace('/+?\d(? :[\s+()-]*\d){5,}/', '[隐藏]', '+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) ) (之前的帖子只回答了部分问题,帖子已关闭: Regex matching phone numbers (with PHP preg_replace)

Since your string contains non-ASCII whitespace characters, you need to use由于您的字符串包含非 ASCII 空白字符,因此您需要使用

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

See the PHP demo .请参阅PHP 演示

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.常规空格替换为\s速记字符 class, u标志用于确保字符串被正则表达式引擎处理为 Unicode 字符串而不是字节字符串,并且\s现在匹配任何 Unicode 空白字符。

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

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