我有字符串"image(100)"
,我希望过滤字符串以删除image
,然后括号只返回数字100
。
我已尝试移除/[^image]/
和image
,但我不知道如何移除parenthesis
。
答案 0 :(得分:4)
只需替换非数字,没有任何内容,就像这样:
echo preg_replace('/\D/', '', 'image(100)');
答案 1 :(得分:1)
您需要使用反斜杠转义括号,因为它们是正则表达式中的特殊字符。在这里,我将它们用于capture数字和save it to the $matches
array。
<?php
$str = "image(100)";
if (preg_match("/image\((\d+)\)/", $str, $matches)) {
echo $matches[1];
}