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