1

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');

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Piggy-backing on this, instead of return $text, why not just return preg_replace.....?
0

\s/\s

And use this tool, it's cool!

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.