note 101938 added to function.preg-replace
| From: | nospam at probackup dot nl | Date: | Wed, 19 Jan 2011 13:03:35 +0000 |
| Subject: | note 101938 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175887@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 217.19.30.100
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/101938
Del: integrated -- https://master.php.net/note/delete/101938/integrated
Del: useless -- https://master.php.net/note/delete/101938/useless
Del: bad code -- https://master.php.net/note/delete/101938/bad+code
Del: spam -- https://master.php.net/note/delete/101938/spam
Del: non-english -- https://master.php.net/note/delete/101938/non-english
Del: in docs -- https://master.php.net/note/delete/101938/in+docs
Del: other reasons-- https://master.php.net/note/delete/101938
Reject -- https://master.php.net/note/reject/101938
Search -- https://master.php.net/manage/user-notes.php