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


the first lines only clear the line.

The last line serves as a replacement.



Switching options around should do the trick

$result = preg_replace("/\-/", " ", $result);

+2


source


This will help you:

$output = str_replace(',', ' ', $result);

      

+1


source


$result = str_replace('-', ' ', $result);

=> - into spaces

$result = str_replace(' ', '-', $result);

=> spaces -

+1


source







All Articles