I have string "image(100)"
, i want filter the string to remove image
and the parentheses then return just the number 100
.
i have tried /[^image]/
and image
removed, but i don't know how to remove parenthesis
.
I have string "image(100)"
, i want filter the string to remove image
and the parentheses then return just the number 100
.
i have tried /[^image]/
and image
removed, but i don't know how to remove parenthesis
.
To just replace non-digits, with nothing, it's like this:
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.
<?php
$str = "image(100)";
if (preg_match("/image\((\d+)\)/", $str, $matches)) {
echo $matches[1];
}