<?php
$username = "@#*(#(*#($&*#$";
if (preg_match("/^[0-9a-z-A-Z-_@ !]*/", $username))
{
echo "true";
} else {
echo "false";
}
?>
<?php $username = @#*(#(*#($&*#$; if (preg_match(/^[0-9a-z-A-Z-_@ !]*/, $username)) { echo true; } else {
Share
Two things:
-within[...]is a special character indicating a range and needs to be escaped; and\wis a shortcut for[A-Za-z0-9_]. You should use it (although not using it isn’t technically a problem here).So: