note 101938 deleted from function.preg-replace by crell
| From: | crell@php.net | Date: | Fri, 01 Jul 2022 19:52:15 +0000 |
| Subject: | note 101938 deleted from function.preg-replace by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218865@lists.php.net to get a copy of this message | ||
Note Submitter: nospam at probackup dot nl
----
Warning: a common made mistake in trying to remove all characters except numbers and letters from a
string, is to use code with a regex similar to preg_replace('[^A-Za-z0-9_]', '',
...). The output goes in an unexpected direction in case your input contains two double quotes.
echo preg_replace('[^A-Za-z0-9_]', '', 'D"usseldorfer
H"auptstrasse')
D"usseldorfer H"auptstrasse
It is important to not forget a leading an trailing forward slash in the regex:
echo preg_replace('/[^A-Za-z0-9_]/', '', 'D"usseldorfer
H"auptstrasse')
Dusseldorfer Hauptstrasse
PS An alternative is to use preg_replace('/\W/', '', $t) for keeping all alpha
numeric characters including underscores.