<?php $string = 'w/'; if (preg_match('~[^0-9a-z]~i', $string)) { print 'Only a-z0-9 allowed'; } else { print 'Apparently valid.'; }