Allow only certain characters in a string
3 answers
Use a regular expression. Use preg_match () .
$matches = preg_match('/[^a-z0-9]/', $string);
So, if it $matches
does 1
, you know what the $string
bad characters contain. Otherwise $matches
- 0
, and $string
- OK.
+1
source to share
To mix things up a little
<?
$input = "hello world 123!";
$digits = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
if (ctype_alnum($input))
{
if (ctype_lower(str_replace($digits, "", $input)))
{
// Input is only lowercase and digits
}
}
?>
But regex is probably the way to go! =)
0
source to share