Question

How to check if the string has alphabets in PHP

The ctype_alpha and ctype_digit doesn't help in it. is their any method?

Was it helpful?

Solution

You can use preg_match for this.

if(preg_match("/[a-z]/i", $string)){
    print "it has alphabet!";
}

If you want to check for all the characters to be alphabet, then use anchor around the regex. For example ^[a-z]+$.

OTHER TIPS

Posted as requested

/^[a-zA-Z]+$/ alpha, /^[0-9]+$/ num, /^[a-zA-Z0-9]+$/ alpha-num

or

/^\pL+$/u Uni alpha, /^\pN+$/u Uni num, /^[\pL\pN]+$/u Uni alpha-num

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top