php preg_replace phone 带空格的数字
php preg_replace phone number with spaces
我有以下代码可以在字符串中找到一个数字并将其设为粗体。但是,如果 phone 数字中有空格,b 标签会被多次添加。我需要修改什么以允许空格出现在 phone 数字中的任何位置?
$text = preg_replace('/(\d+)/', '<b></b>', $text);
使用这个正则表达式:
(\d[\d\s]*)(?=\s+)
工作原理:
(
\d # First digit
[\d\s]* # Any more digits or whitespace
)
(?=\s+) # To make sure not to Capture last whitespace
它不会捕获最后一个 space,因此您不会以 <b> 123 </b>
结束,而是以 <b>123</b>
结束
这个 RegEx 也可以工作:
/([\d\s]+)/
然而,如果没有数字,这将匹配字符串,只有 spaces.
我有以下代码可以在字符串中找到一个数字并将其设为粗体。但是,如果 phone 数字中有空格,b 标签会被多次添加。我需要修改什么以允许空格出现在 phone 数字中的任何位置?
$text = preg_replace('/(\d+)/', '<b></b>', $text);
使用这个正则表达式:
(\d[\d\s]*)(?=\s+)
工作原理:
(
\d # First digit
[\d\s]* # Any more digits or whitespace
)
(?=\s+) # To make sure not to Capture last whitespace
它不会捕获最后一个 space,因此您不会以 <b> 123 </b>
结束,而是以 <b>123</b>
这个 RegEx 也可以工作:
/([\d\s]+)/
然而,如果没有数字,这将匹配字符串,只有 spaces.