On 06/04/06, Angelo Zanetti <[EMAIL PROTECTED]> wrote:
>
> Anyway for a simple query, if I just wanted to check that a variable has only 
> text and numeric characters would I do something like this
> (i want it to fail if it finds a symbol eg: + - { " etc...):
>
> echo  "REG result: " . preg_match('/[a-zA-Z0-9]*/', '{d-fg');
>
> however this resolves to true because there are normal characters 
> (alphabetical characters) so how do I make the expression fail because of the 
> { and - characters in the string?
>
> You can also list which characters you DON'T want -- just use a '^' as the 
> first symbol in a bracket expression
> (i.e., "%[^a-zA-Z]%" matches a string with a character that is not a letter 
> between two percent signs). But that would mean that I would have to list 
> each symbol I dont want and that would be
> undesireable as it would be better to list exactly what the acceptable 
> characters are.

You're pretty much there. Your first example resolved to true because
you didn't anchor the start and end of the expression.

    /^[a-z0-9]*$/i

It would be a bit more efficient to use the negated character class
you mentioned.

  /[^a-z0-9]/i

All you need to bear in mind is that preg_match() would then return
true if the string contains an illegal character and false if it is OK
rather than the other way around.

So these should both produce the same result:

preg_match('/^[a-z0-9]*$/i', '{d-fg');
!preg_match('/[^a-z0-9]/i', '{d-fg');

  -robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to