Replace dash in space using php
You can help me with my problem.
I want to create a function to replace space in dash, then I found a solution for this
this is the sulotion i found:
function slug($phrase, $maxLength=100000000000000)
{
$result = strtolower($phrase);
$result = preg_replace("/[^A-Za-z0-9\s-._\/]/", "", $result);
$result = trim(preg_replace("/[\s-]+/", " ", $result));
$result = trim(substr($result, 0, $maxLength));
$result = preg_replace("/\s/", "-", $result);
return $result;
}
my problem is i want to create the other way around to replace the dash with a space
example input:
shooting-space
output
dash in space
How can i do this?
+3
source to share
4 answers
You can use this description based on preg_replace
:
$result = preg_replace('/(?<!\s)-(?!\s)/', ' ', $input);
Demo version of RegEx
This will avoid replacing it -
with a space if it is surrounded by a space.
+2
source to share