简体   繁体   English

正则表达式使电话号码变粗 - 并不总是有效

[英]regular expression to make phone numbers bold - doesn't always work

I need to wrap bold tags around any phone number within a string. 我需要在字符串中的任何电话号码周围包裹粗体标签。 I have the following regex 我有以下正则表达式

/(\d[\d\s]*)(?=\s+)/

which works for this: 适用于此:

Call us on 0800 2458 452 now! 请致电0800 2458 452联系我们!

but not this: 但不是这个:

Call us now on 0800 2458 452 请致电0800 2458 452联系我们

So if the number occurs at the end of the string the regex does not work properly. 因此,如果数字出现在字符串的末尾,则正则表达式无法正常工作。 It outputs the string as which misses the bold from the last three digits. 它输出的字符串错过了最后三位数字的粗体。

Call us now on <b>0800 2458</b> 452

Can anyone see what is wrong with the code? 任何人都可以看到代码有什么问题吗?

$bold_text = preg_replace('/(\d[\d\s]*)(?=\s+)/', '<b>$1</b>', $text);

use word boundary instead of space 使用单词边界而不是空格

(\d[\d\s]*)(?=\b)

and better to add the same before 并且最好在之前添加相同的内容

(?<=\b)(\d[\d\s]*)(?=\b)

to don't match B52 与B52不匹配

Your lookahead pattern requires at least 1 whitespace to appear after the phone number. 您的前瞻模式要求在电话号码后面至少显示1个空格。 You may "unroll" your pattern to match at the word boundaries: 您可以“展开”您的模式以匹配单词边界:

\b\d+(?:\s+\d+)*\b

See the regex demo 请参阅正则表达式演示

Or use the unambiguous word boundaries - (?<!\\w) and (?!\\w) : 或者使用明确的单词边界 - (?<!\\w)(?!\\w)

(?<!\w)\d+(?:\s+\d+)*(?!\w)

The pattern matches: 模式匹配:

  • (?<!\\w) - no word character before the digit (?<!\\w) - 数字前没有单词字符
  • \\d+ - 1+ digits \\d+ - 1+位数
  • (?:\\s+\\d+)* - zero or more sequences of 1+ whitespaces followed with 1+ digits (?:\\s+\\d+)* - 1个以上空格的零个或多个序列,后跟1个数字
  • (?!\\w) - not followed with a word (alphanumeric/underscore) character. (?!\\w) - 后面没有单词(字母数字/下划线)字符。

See this regex demo 看到这个正则表达式演示

IDEONE PHP demo : IDEONE PHP演示

$re = '~(?<!\S)\d+(?:\s+\d+)*(?!\S)~'; 
$strs = array("Call us on 0800 2458 452 now!","Call us now on 0800 2458 452"); 
foreach ($strs as $str) {
 echo preg_replace($re, '<b>$0</b>', $str) . PHP_EOL;
}

GOTCHA: FLOAT NUMBERS GOTCHA:FLOAT NUMBERS

If you want to make sure you do not match floats with the regex, use 如果要确保浮点数与正则表达式不匹配,请使用

(?<!\w|\d\.)\d+(?:\s+\d+)*(?!\w|\.\d)

See the regex demo 请参阅正则表达式演示

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

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