I have strings containing the runtime of movies:
1 h 19 min
Now I'd like it to look like this:
1h 19m
I know this should be actually pretty easy but I just don't manage to find a solution.
You can remove all spaces between a digit and a non-digit with a regular expression:
$str = preg_replace('/(?<=\d) *(?=\D)/', '', $str);
Update: Unfortunately this does not turn "min" into "m", which I didn't actually notice is desired.
In light of this, a str_replace
solution with arrays such as bsdnoobz's is really the only sane way to go here. But just for the fun of it:
$str = preg_replace('/(?<=\d) *(\D)\S*/', '$1', $str);
$runtime="1 h 19 min";
$runtime=str_replace(" h","h",$runtime);
$runtime=str_replace(" min","m",$runtime);
$str = str_replace(array(' h', ' min'), array('h', 'm'), $str);
The easiest way
$runtime = "1 h 19 min";
$runtime = str_replace(" h", "h", $runtime);
$runtime = str_replace(" min", "min", $runtime);