简体   繁体   中英

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!

but not this:

Call us now on 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

Your lookahead pattern requires at least 1 whitespace to appear after the phone number. 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)\d+(?:\s+\d+)*(?!\w)

The pattern matches:

  • (?<!\\w) - no word character before the digit
  • \\d+ - 1+ digits
  • (?:\\s+\\d+)* - zero or more sequences of 1+ whitespaces followed with 1+ digits
  • (?!\\w) - not followed with a word (alphanumeric/underscore) character.

See this regex demo

IDEONE PHP demo :

$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

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

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