Currently I am using ctype_alnum
to check for alphanumeric type in csv file. But one of my data has white spaces between them and so ctype_alnum
gives me false.
Example: ctype_alnum("Here is 23");
So my question is how can I check for white spaces in string in php ?
You can use:
// returns true if $str has a whitespace(space, tab, newline..) anywhere in it.
function has_whitespace($str) {
return preg_match('/\s/',$str);
}
Or you can write you own function to check if a string contains only alphabets,digits,spaces:
// returns true if $str has nothing but alphabets,digits and spaces.
function is_alnumspace($str){
return preg_match('/^[a-z0-9 ]+$/i',$str);
}
So you want to check if a string consists of a letter (a-z) ignoring case (A-Z) or horizontal white space (space(0x20) and tab(0x09))? That would be:
if (preg_match('/^[a-z\h]+$/i', $string)) {