note 92443 added to function.preg-replace

From: Date: Fri, 24 Jul 2009 10:56:24 +0000
Subject: note 92443 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-158680@lists.php.net to get a copy of this message
Maybe it's better to avoid using \w and \W character class matches because they behave differently depending on locale server settings, therefore you will end up with a regular expression match that changes its behavior depending upon server settings. <?php /* according to most websites the regexp sintax [\w] is identical to [A-Za-z0-9_] See: http://en.wikipedia.org/wiki/Regular_expression http://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx */ $result1 = preg_replace('/[A-Za-z0-9_]*/', '', "test àèìòù test"); $result2 = preg_replace('/\w*/', '', "test àèìòù test"); echo "<pre>[" . $result1 . "]</pre>"; //ok, it shows: "[ àèìòù ]" echo "<pre>[" . $result2 . "]</pre>"; //WARNING on server other than UK/US locale, it shows: "[ ]" /* Why does this happen? preg_replace uses Perl Compatible Regular Expressions and NOT POSIX regular exp sintax. Therefore a minor difference is that in the former one the \w charcater class matches also the accented characters depending on locale settings. See: http://perldoc.perl.org/perlre.html#Regular-Expressions */ ?> ---- Server IP: 213.92.11.19 Probable Submitter: 151.16.81.68 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/92443 Del: integrated -- https://master.php.net/note/delete/92443/integrated Del: useless -- https://master.php.net/note/delete/92443/useless Del: bad code -- https://master.php.net/note/delete/92443/bad+code Del: spam -- https://master.php.net/note/delete/92443/spam Del: non-english -- https://master.php.net/note/delete/92443/non-english Del: in docs -- https://master.php.net/note/delete/92443/in+docs Del: other reasons-- https://master.php.net/note/delete/92443 Reject -- https://master.php.net/note/reject/92443 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158680) next »