I am going to read in via XML a string of text that gives a time in minutes and seconds as below:
“79 minutes 43 seconds”
But I need to extract the minutes.
The minutes could be a 1,2,3 or 4 figure number with the word “minutes” after a space, then the seconds which I am not interested in.
How could I extract the minutes figure into a separate string using PHP?
You can use the
(int)cast to convert any leading digits of a string into a number:This approach is limited to strings that start with a number and this number always means minutes. For other cases, it’s probably better to use regular expressions.