awsm awsm - 1 month ago 18
PHP Question

Remove plus sign (+) from string

I am trying to use

preg_replace()
to remove a plus sign (
+
) from my string. I used

$variation = preg_replace('/[^\p{L}\p{N}\s]/u', '', $variation);


But that removed periods (
.
) too, which I need it in the string. Is there a way to just remove the plus sign?

Answer

Although the original answer to this question does achieve the intended effect, it is not the most efficient way to do this simple task. As noted in the comments above, the use of str_replace() is preferred in this case.

$variation = str_replace("+", "", $variation);

ORIGINAL ANSWER:

This works to remove only a plus sign:

$variation = preg_replace(/[+]/, "", $variation);

You can see it work here: http://www.phpliveregex.com/p/1Fb (be sure you select the preg_replace function)