I came across a need today to use str_replace() to replace all hyphens in a string with spaces. Easy enough:
$string = "Some-test-string";
$string = str_replace('-', ' ', $string);
The thing is, using ' ' (string with empty space) in the "replacement" part of str_replace() just feels so dirty. Also seems brittle... Looks ugly too.
Is there no better option? I tried using a regex pattern in the "replacement" section but that didn't work, it came out literal.
Ideally, something like this would be great if possible:
$string = "Some-test-string";
$string = str_replace('-', '/\s/', $string);
Thanks!
Edited: Replaced all preg_replace() calls with str_replace() per suggestion.
preg_replace("/[-]+/", "\x20", $string);, but I don't know if that makes it cleaner\sis a space, tab, or new line, there's no way for PHP to know which character you are expecting there (or no way that I know of). The' 'should be fine, you could input the HTML entity if this is for a browser, , or .