我正在尝试构建一个正则表达式来检测错误的电话号码,并将其替换为原始电话号码。我有电话号码,在一个带有文本的文本字段中(带有html标签)是错误的号码(出于某种原因,网站用户喜欢在号码之间插入各种字符)。以下是我正在使用的,它在大多数情况下都很好,但不是全部:
$phone = '0888123123';
$text = 'Some html text with variants of the phone number in wrong format -
0~8~8~8~1~2~3~1~2~3
0.8.8.8.1.2.3.1.2.3
0 8 8 8 1 2 3 1 2 3
0888-123-123
The last format does not work!';
preg_match_all('/('.implode('['D]+', str_split($phone)).')/i', strip_tags($text), $matches);
if (count($matches) > 0) {
foreach($matches as $value) {
$text = str_replace($value, $phone, $text);
}
}
我试图在数字之间找到任何非数字字符,但只有当该非数字字符在所有数字之间时才有效,即0.8.8.8.1.2.3.1.2.3,但它与0888-123-123不匹配。在这个正则表达式中是否添加了一些内容来捕获其余的情况?
[^'d'n]
你可以试试这个。请参阅演示。
http://regex101.com/r/nB2lL9/6