note 60111 deleted from function.strspn by mazzanet
| From: | mazzanet@php.net | Date: | Tue, 27 Dec 2005 09:25:40 +0000 |
| Subject: | note 60111 deleted from function.strspn by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100771@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.
finds the position of the first of any characters on the pattern, against string lenght.
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="asdfASDF123/-_~.";
if (strlen($path) != strspn($path,$allowed)) {
echo "invalid path!";
}
// THANKS
?>
another variant, assuming a value diffrent to zero as TRUE:
<?php
if (strlen($email)-strspn($email,$"qwerty123@._")) {
echo "dont try to fool me, invalid email";
}
?>