PHP bandwidth punctuation supports apostrophe

Like PHP bandwidth punctuation , but I want to keep an apostrophe. Example:

I'm a student, but I don't like the school. I'll quit school.

The line after the strip should be:

I'm a student but I don't like the school I'll quit school

How can I do this with regex or other ways?

+3
source share
2 answers

If you want to also support all Unicode punctuation characters, use this regex:

$str = preg_replace("#((?!')\pP)+#", '', $str);

This regular expression matches the character character of the Unicode prefix character \pP, and matching will escape the apostrophe character using a negative result.

+3
source

Here is an adaptation of the first example:

$string = preg_replace('/[^a-z0-9\' ]+/i', '', $string);
+2
source

All Articles