tags:

views:

64

answers:

2

Hi,

I need a regex expression (PCRE) to match a integer number inside a string, being the string like : image89.jpg

I've tried many options without success.

I'm using preg_replace() by the way

My last attempt :

preg_replace('(\d+)', '$1', 'image89.jpg');
+3  A: 
preg_match('/\d+/', 'image89.jpg', $matches);
$digit = $matches[0];

If you plan to find multiple sets of digits in the same string, you'll want to use preg_match_all.

eyelidlessness
+1  A: 

You need to wrap your regular expression in delimiters (more on those here). I'd say an easier way to do it is to discard anything that isn't a digit:

preg_replace('/[^\d]+/', '', 'image89.jpg');

Assuming you have to use preg_replace() instead of the more suitable preg_match(), that is (see eyelidlessness' answer for that).

BoltClock