Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validiate for a solely alphabetic string with spaces in PHP?

Tags:

regex

php

I know that there is the function ctype_alpha, though this one will return FALSE when the string contains spaces (white space character).

How do I allow alpha characters and spaces, but nothing else?

like image 607
Carpet Avatar asked Apr 22 '12 04:04

Carpet


2 Answers

$is_alpha_space = ctype_alpha(str_replace(' ', '', $input)));

or

$is_alpha_space = preg_match('/^[a-z\s]*$/i', $input);
like image 53
dtbarne Avatar answered Nov 14 '22 23:11

dtbarne


if (preg_match("^/[a-zA-Z ]+$/", $input)) {
    // input matches
}

Demo: http://ideone.com/jp6Wi
Docs: http://php.net/manual/en/function.preg-match.php

like image 34
Matt Ball Avatar answered Nov 14 '22 23:11

Matt Ball