Re: Ereg problems
| From: | Niel Archer | Date: | Tue, 16 Jan 2007 20:37:07 +0000 |
| Subject: | Re: Ereg problems | ||
| References: | 1 2 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-27395@lists.php.net to get a copy of this message | ||
Hi
As I said before I don't use posix regexes (i.e. ereg), as they're not
binary safe and can be much slower than the PCRE versions. the PCRE
equivalent of what you want is:
if (preg_match("/^[-A-Za-z0-9_' ]+$/", $string)}:
echo "string valid";
else:
echo "string invalid";
endif;
The "A-Za-z0-9_" can be simplified to "\w" meaning any 'word'
character.
The slashes '/' are required as delimeters around a regex with PCRE.
The '^' and '$' anchor to beginning and end. This makes sure you're
testing the whole string not just a part of it, otherwise it would match
up to the first invalid character but still make a match.
I use this kind of test frequently myself, so I know it is possible
and fairly simple to do.
Niel