xProteuSx Posted August 19, 2012 Share Posted August 19, 2012 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? Quote Link to comment Share on other sites More sharing options...
.josh Posted August 19, 2012 Share Posted August 19, 2012 // method 1 $time=array_filter(preg_split('~[a-z]+~i',$string)); // method 2 preg_match_all('~[0-9]+~',$string,$time); $time = $time[0]; Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted August 19, 2012 Author Share Posted August 19, 2012 Wow, that is beautiful. Thanks a lot. One day I will learn this regex stuff! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.