note 53613 added to function.preg-match
| From: | webmaster at m-bread dot com | Date: | Tue, 07 Jun 2005 12:47:25 +0000 |
| Subject: | note 53613 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90055@lists.php.net to get a copy of this message | ||
If you want to get all the text characters from a string, possibly entered by a user, and filter out
all the non alpha-numeric characters (perhaps to make an ID to enter user-submitted details into a
database record), then you can use the function below. It returns a string of only the alpha-numeric
characters from the input string (all in lower case), with all other chracters removed.
<?php
function removeNonAN($string){
preg_match_all('/(?:([a-z]+)|.)/i', $string, $matches);
return strtolower(implode('', $matches[1]));
};//EoFn removeNonAN
?>
It took me quite a while tocome up with this regular expression. I hope it saves someone else that
time.
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53613
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53613&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53613&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53613&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53613&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53613&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53613&report=yes
Search -- http://master.php.net/manage/user-notes.php