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? Link to comment https://forums.phpfreaks.com/topic/267300-find-all-single-and-multi-digit-numbers-in-a-string/ 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]; Link to comment https://forums.phpfreaks.com/topic/267300-find-all-single-and-multi-digit-numbers-in-a-string/#findComment-1370622 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! Link to comment https://forums.phpfreaks.com/topic/267300-find-all-single-and-multi-digit-numbers-in-a-string/#findComment-1370623 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.