Niels
Niels

Reputation: 373

Regex for space slash space

I want to match any instance of [space][slash][space].

ie.

" / "

in a regex pattern.

I can't find the answer anywhere. What have I missed?

function madeby_remove_slash($text) {
    preg_replace('/ \/ /', ' ', $text);
    return $text;
}
echo madeby_remove_slash('This is the / text');

Upvotes: 1

Views: 2614

Answers (3)

Adrian
Adrian

Reputation: 2875

Try removing the enclosing forward-slashes from your regex. AFAIK they're not necessary in PHP, and it may well be thinking that you're asking it to match those as well.

Upvotes: -1

complex857
complex857

Reputation: 20753

You don't assign the return value of the preg_replace to the $text variable in your function.

function madeby_remove_slash($text) {
    return preg_replace('/ \/ /', ' ', $text); // return what the preg_replace returns
}

or if you want to replace a literal string you can use str_replace too.

str_replace(' / ', ' ', $text); // this works too

Upvotes: 3

adseipsum
adseipsum

Reputation: 43

\s/\s

And use this tool, it's cool!

Upvotes: 0

Related Questions