PPjyran
PPjyran

Reputation: 95

replace dashes in to space using php

can you help me in my problem.

i want to create a function to replace the space into dash then i found a solution for that

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 a vice versa for that replace dash to space

example input:

dash-to-space

example output

dash to space

how can i do that?

Upvotes: 4

Views: 320

Answers (4)

FrancoisBaveye
FrancoisBaveye

Reputation: 1902

$result = str_replace('-', ' ', $result); => - to spaces

$result = str_replace(' ', '-', $result); => spaces to -

Upvotes: 2

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27242

It will help you :

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

Upvotes: 2

anubhava
anubhava

Reputation: 786289

You can use this lookaround based preg_replace:

$result = preg_replace('/(?<!\s)-(?!\s)/', ' ', $input);

RegEx Demo

This will avoid replacing - by space if it is surrounded by space.

Upvotes: 3

RST
RST

Reputation: 3925

the first lines are only cleaning up the string.

The last line performs the replace function.

switching the parameters around should do the trick

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

Upvotes: 3

Related Questions