note 60110 deleted from function.strspn by nlopess
| From: | nlopess@php.net | Date: | Wed, 28 Dec 2005 16:29:22 +0000 |
| Subject: | note 60110 deleted from function.strspn by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100864@lists.php.net to get a copy of this message | ||
Note Submitter: AT-HE (at_he AT hotm4il DOT com)
----
USEFUL TO FIND FORBIDDEN CHARACTERS IN A STRING, according to a pattern!, in conjunction with string
length.
useful to catch illegal characters in a string, e.g. in names, forms, filenames, emails, numbers,
urls, etc.. examples:
<?php
//example one: natural numbers
$allowed="0123456789";
if ( strlen($number) != strspn($number,$allowed) ) {
echo "i said only numbers!";
}
//example two: email (with some characters)
$allowed="qwertyasdf123@.";
if ( strlen($email) != strspn($email,$allowed) ) {
echo "invalid email!";
}
// examle three: filenames or urls (with some characters)
$allowed="qwertyasdfASD123-_~.";
if ( strlen($filename) != strspn($filename,$allowed) ) {
echo "invalid filename!";
}
// THANKS
?>