note 57839 added to function.ereg

From: Date: Sun, 16 Oct 2005 08:13:52 +0000
Subject: note 57839 added to function.ereg
Groups: php.notes 
Request: Send a blank email to php-notes+get-97076@lists.php.net to get a copy of this message
A common mistake seems to be trying to escape characters within a bracket expression. Unlike the preg functions, backslash is always taken literally within a bracket expression using the ereg functions. See http://php.planetmirror.com/manual/en/function.eregi.php#57824 for more details. Some of the posts here can be re-written to be much simpler. 16-Feb-2005 10:02 attempts to allow square brackets in a string with ^[a-zA-Z0-9 [.[.] [.].] ]{1,}$ Although this appears to work a less confusing means is ^[]a-zA-Z0-9[]{1,}$ The ] has to be the first character (after a possible ^) but the [ can be anywhere as long as it is not in the middle of a range of course. 09-Apr-2005 11:52 Says that ereg("hi[:space:]*bob", $string) doesnt work in php 4 and to use preg_match() instead. The above quoted use is incorrect it should be <?php ereg("hi[[:space:]]*bob", $string); ?> I tested this with the following in php 4.3.3 and it works fine <?php //The hex codes are space, tab, line feed, vertical tab, form feed, carriage return $whitespace = "\x20\x09\x0a\x0b\x0C\x0d"; $teststring = "hi".$whitespace."bob"; $result = ereg ("hi[[:space:]]*bob", $teststring, $arr); echo ('Matches '.$result.' characters'); //Prints Matches 11 characters ?> 23-May-2005 08:22 Says that ereg("^[' A-Za-Z]+$", $cardName); will not work. The fault with the above is the range a-Z the capital Z comes before small a and so this will fail. The following works fine <?php $cardname = "John 'Doe'"; $result = ereg("^[' A-Za-z]+$", $cardname, $arr); echo ('Matches '.$result.' characters'); //Prints Matches 10 characters ?> 09-Sep-2005 11:01 Tries to escape with \ in a bracket expression You cannot with ereg functions (preg you can) so ereg("^([-a-zA-Z0-9_\.\!@#\$&\*\+\=\|])*$" , $var) should be <?php ereg("^([-a-zA-Z0-9_.!@#$&*+=|])*$", $var); ?> ---- Manual Page -- http://www.php.net/manual/en/function.ereg.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57839 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57839&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57839&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57839&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57839&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57839&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57839&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57839&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57839&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97076) next »