PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
lizlazloz
Forum Commoner
Posts: 64 Joined: Mon Dec 29, 2003 7:29 am
Post
by lizlazloz » Fri Jan 23, 2004 2:39 am
i tried
Code: Select all
<?php
$chars=[a-z0-9];
if ( preg_match($chars, $name) )
{
do stuff....
}
?>
but it didnt work...
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri Jan 23, 2004 4:17 am
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