note 60111 added to function.strspn
| From: | (at_heAThotm4ilDOTcom) | Date: | Tue, 27 Dec 2005 07:58:14 +0000 |
| Subject: | note 60111 added to function.strspn | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100765@lists.php.net to get a copy of this message | ||
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";
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.strspn.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60111
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60111&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60111&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60111&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60111&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60111&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60111&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60111&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60111&report=yes
Search -- http://master.php.net/manage/user-notes.php