I'm fairly weak on regex and this is giving me a headache to try and figure out. I'm just trying to extract the time from the string and modify the text of the time. I get strings in the following forms:
"Some text I don't care about | 2.3 seconds ago"
"Some text I don't care about | 5.2 minutes ago"
"Some text I don't care about | 7.0 hours ago"
"Some text I don't care about | 1.9 days ago"
I would like to replace the above strings to they are in the forms respectively:
"2.3 secs"
"5.2 mins"
"7.0 hrs"
"1.9 days"
I know basics of replacing in regex, but to remove multiple things in front and behind the text that I want, while replacing "hours" with "hrs", etc is beyond my regex skill level.
Any help would be appreciated.
Thanks.
EDIT:
Well after messing about I came to a solution using multiple different functions. I don't exactly like it, but it works. I would prefer a single preg_replace solution if possible. Here is my current ugly solution:
$testString = "This is text I don't care about | 7.3 seconds ago";
$testString = array_shift(array_reverse(explode('|', $testString)));
$pattern = array('/seconds/', '/minutes/', '/hours/', '/ago/');
$replace = array('secs', 'mins', 'hrs', '');
$testString = trim(preg_replace($pattern, $replace, $testString));
Output is: 7.3 secs