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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.