Page 1 of 1
only allow specified characters...
Posted: Fri Jan 23, 2004 2:39 am
by lizlazloz
i tried
Code: Select all
<?php
$chars=[a-z0-9];
if ( preg_match($chars, $name) )
{
do stuff....
}
?>
but it didnt work...
Posted: Fri Jan 23, 2004 4:17 am
by twigletmac
Try:
Code: Select all
<?php
$name = 'ted3';
$chars = 'a-z0-9';
if (preg_match('/^['.$chars.']+$/i', $name)) {
echo 'yes';
} else {
echo 'no';
}
?>
if you're only testing for alphanumeric characters though, you can avoid the regex and simply use [php_man]ctype_alnum[/php_man]():
Code: Select all
<?php
$name = 'tede4$3';
$chars = 'a-z0-9';
if (ctype_alnum($name)) {
echo 'yes';
} else {
echo 'no';
}
?>
Mac