-1

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Muh Ghazali Akbar
  • 1,169
  • 3
  • 13
  • 21
  • A character class is a listing of allowed/disallowed characters. It is not a series of characters. `[^image]` allows for a non `i`, `m`, `a`, `g`, or `e`. You could do `[^\d]+`, or just use PHPglue's answer below. – chris85 Apr 04 '17 at 00:39
  • 2
    Possible duplicate of [Remove non-numeric characters (exlcuding periods and commas) from a string](http://stackoverflow.com/questions/4949279/remove-non-numeric-characters-exlcuding-periods-and-commas-from-a-string) – Mocking Apr 04 '17 at 01:39

2 Answers2

4

To just replace non-digits, with nothing, it's like this:

echo preg_replace('/\D/', '', 'image(100)');
StackSlave
  • 10,613
  • 2
  • 18
  • 35
1

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];
}
miken32
  • 42,008
  • 16
  • 111
  • 154