我正在尝试使用 preg_replace 删除低于“15”的数字:
$string = '1 20 5 16 11 15 14';
我期望 preg_replace 后的输出为
20 16 15
我该怎么做?
使用这个模式
\s*\b\d\b|\b1[01234]\b
说明
\s # <whitespace character>
* # (zero or more)(greedy)
\b # <word boundary>
\d # <digit 0-9>
\b # <word boundary>
| # OR
\b # <word boundary>
1 # "1"
[01234] # Character in [01234] Character Class
\b # <word boundary>
替换
0
到 9
或 10
到 14
范围内的所有数字
$string = '1 20 5 16 11 15 14';
var_dump(preg_replace("/\b([0-9]{1}|1[0-4]{1})\b/", "", $string));
// the output: ' 20 16 15 '