Page 1 of 1

Checking a string

Posted: Mon Aug 02, 2004 4:58 pm
by dwfait
Hi again. How would i check a variable if it contained any special characters? IE + ! " £ $ % ^ & * ( ) ; ' # ] [ , . / \ ¬ ` | ¦ { } ~ @ : ? > < - =.

Posted: Mon Aug 02, 2004 5:00 pm
by tim
any type of Regular Expression would do the job well.

preg_match and all its forms.
ereg and all its forms would be what to look at.

Posted: Mon Aug 02, 2004 5:28 pm
by dwfait
umm..can you clarify and/or give me an example of the syntax? i cant find anything for it...

Posted: Mon Aug 02, 2004 6:49 pm
by tim
well, can you tell us what you are using this for?

Is this for a username sign-up script and you want to eliminate the possibilty of them using special characters?

This data could be helpful in assiting you. Depending on what your using this for, this regEx would only allow characters 0-9, a-z(capital or lowercase) to be allowed as well as a underscore and -.

Code: Select all

<?php
if (!ereg("^[_a-zA-Z0-9-]+$", $name)) {   
   echo "no special chars allowed.";
} else {
// do whateevr
}
?>

Posted: Mon Aug 02, 2004 6:51 pm
by tim
and if you wish to avoid regEx as much as possible (some people just hate them)

read: http://us2.php.net/manual/en/ref.ctype.php

But if this is not what your looking for, please let us know what you are wishing to accomplish