Mikey1980
Mikey1980

Reputation: 1001

preg_replace(), removing string containing '+' char

I am having an issue removing "%3Cbr+%2F%3E" from my string using the preg_replace function. My assumption is that the '+' char is being interpreted incorrectly. Here my code:

$address = preg_replace('/%3Cbr+%2F%3E/', '', urlencode($address));

Thanks as always!

Upvotes: 0

Views: 154

Answers (1)

Gumbo
Gumbo

Reputation: 655229

The + is a special character in regular expression. It is a quantifier and means that the preceding expression can be repeated one or more times.

Escape it with \+ and it should work:

$address = preg_replace('/%3Cbr\\+%2F%3E/', '', urlencode($address));

But since you’re replacing a static expression, you could also use str_replace:

$address = str_replace('%3Cbr+%2F%3E', '', urlencode($address));

Upvotes: 6

Related Questions