[英]How to match some characters with a regular expression?
I have string "image(100)"
, i want filter the string to remove image
and the parentheses then return just the number 100
. 我有字符串
"image(100)"
,我想过滤字符串以删除image
,然后括号就只返回数字100
。
i have tried /[^image]/
and image
removed, but i don't know how to remove parenthesis
. 我已经尝试过
/[^image]/
并删除了image
,但是我不知道如何删除parenthesis
。
要只替换非数字,什么都没有,就像这样:
echo preg_replace('/\D/', '', 'image(100)');
You need to escape parentheses with a backslash, as they are special characters in regular expressions. 您需要使用反斜杠对括号进行转义,因为它们是正则表达式中的特殊字符。 Here I use them to capture the number and save it to the
$matches
array . 在这里,我使用它们来捕获数字并将其保存到
$matches
数组中 。
<?php
$str = "image(100)";
if (preg_match("/image\((\d+)\)/", $str, $matches)) {
echo $matches[1];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.