Jump to content

Find all single and multi-digit numbers in a string


xProteuSx

Recommended Posts

I am not good with regex, and I can't seem to find a solution to my problem either on these awesome forums, or on Google.

 

I have strings, such as the following:

 

$string1 = 'P20DT13H8M46S';

$string2 = 'P8DT8H14M3S';

 

The first number is a day (D), then there's a hour (H), minute (M), and second (S)

 

So, $string1 is roughly translated to P 20 Days T 13 Hours 8 Minutes 46 Seconds.

 

What I would like is to write a regex expression that extracts the numbers from each string, whether they are single or double digit to produce an array like this:

 

echo awesomeFunction($string1); // results in 20, 13, 8, 46

echo awesomeFunction($string2); // results in 8, 8, 14, 3

 

Any idea on how to do this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.