note 57309 added to function.ereg

From: Date: Fri, 30 Sep 2005 09:27:37 +0000
Subject: note 57309 added to function.ereg
Groups: php.notes 
Request: Send a blank email to php-notes+get-96032@lists.php.net to get a copy of this message
QUOTE:" Beware that eregs in PHP4 are broken, for instance if you want to match "a to z, 0,9 and -" you might want to use [a-z0\-9] but it will silently fail due to a bug : the \ don't escape some special chars, so this regexp is equivalent to [a-z0-9] ! Also putting \[ or \] inside the brackets (to match for a bracket) doesn't work, or raises an error, but curiously putting the \- at the end works. So if you want to match '[a-z\|\[\]]' you have to type '([a-z]|\-|\[\|])' This isn't a misplaced bug report : I put this here so other users don't lose a few hours like I did tracking it down. Workaround : use preg, you'll be faster learning the preg syntax than trying to make ereg work by tweaking the expression. " :ENDOFQUOTE if you want a string to work that matches "a to z", "A to Z", "0 to 9", "-", "[", "]" and "_" you will have to use this expression: ([0-9a-zA-Z_]|\-|\[|\]) Note the difference to the expression in the quote - there is an error in it!!! Instead of ([a-z]|\-|\[\|]) it should be ([a-z]|\-|\[|\]) (the last backslash was misplaced...) Also contrary to what the quoted user wrote i suggest NOT using preg instead - If you want to check an entire string preg won't work. In my case i had to check a string that could have 2 to 15 (special) chars. preg_match only checked the first 2 chars, saw that these were good and stopped... ereg checks the whole string and even more important: returns the whole string in the given array $regs[0] unless parts of it did not match! if you compare $regs[0] with the string you just checked with ereg and they match the whole string passed your requirements. ---- Manual Page -- http://www.php.net/manual/en/function.ereg.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57309 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57309&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57309&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57309&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57309&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57309&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57309&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57309&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57309&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#96032) next »