<?php $string = '/with'; if (preg_match('~^[^0-9a-z]~i', $string)){ echo "only a-Z0-9 is allowed"; //true }