lLets say I have the following: $string = 'New+York-NY and where + is I want a \s and where - is I want ', '. The formatted string would appear as New York, NY.
Here is my code: $string = 'New+York-NY';
$formattedLocation = preg_replace('/[+]/', ' ', $location);
$formattedLocation = preg_replace('/-/', ', ', $formattedLocation);
echo $formattedLocation;
This code works. However it is ugly and I would like to refactor it (if possible). I tried:
$formattedLocation = preg_replace('/[+]/', ' ', $location) && preg_replace('/-/', ', ', $location);
This however doesn't work and is also still ugly. Should I be using a different function? Or should my regex be different? Please don't suggest wrapping it in a custom function (that's not the answer I'm looking for).
Thanks