RE: [PHP] Regular expressions
| From: | Christensen, BruceX R | Date: | Mon, 31 Jul 2000 23:52:28 +0000 |
| Subject: | RE: [PHP] Regular expressions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-9316@lists.php.net to get a copy of this message | ||
How about this?
$legal_char_class = "a-z0-9\.";
$name = eregi_replace("[^$legal_char_class]", "", $name);
That will return $name with all characters not on the "allowed" list removed
from it.
If you'd rather just display an error message if the string contains a
disallowed character, use:
$legal_char_class = "a-z0-9\.";
if (eregi("[^$legal_char_class]", $name)) {
echo "error";
}
The key is the ^, or "not" character. When it is the first char at the
beginning of a regex char class (i.e. inside of []), it matches any
character that is NOT in the list, instead of its normal behavior or
matching characters that ARE in the list.
--Bruce
Bruce Christensen
Intel Corporation
brucex.r.christensen@intel.com
-----Original Message-----
From: Ori Staub [mailto:ori@*NO-SPAM*chikki.net]
Sent: Monday, July 31, 2000 4:29 PM
To: php-general@lists.php.net
Subject: [PHP] Regular expressions
I need help with regular expressions.
I need to find the occurrence of characters in a string, however, for
simplicity, I would like to list them by "what is allowed".
I couldn't get it to work using eregi(), so i devised this little function,
but there must be an easier way! - please help
$legal_char_class = "[a-zA-Z0-9\.]";
$length = strlen($name);
for ($i=0; $i <= $length - 1 ; $i++){
$text_pos = $length - $i;
// break up string into array and test each character
$name_array[$i] = substr($domain_name, -$text_pos,1);
if (!ereg($legal_char_class,$name_array[$i])){
$error = true;
}
}// end for loop
--
Ori Staub
ori@_NO_SPAM_chikki.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net