The ctype_space
method can be used to check if all the characters of the text are only whitespace characters.
ctype_space(mixed $text): bool
This method returns a Boolean
value.
Whitespace characters include
horizontal tab
,vertical tab
,line feed
,carriage return
, andform feed
characters.
<?php$str = " ";printIsSpace($str);$str = "\t\n\r";printIsSpace($str);$str = "A";printIsSpace($str);$str = "a b";printIsSpace($str);function printIsSpace($str) {echo "The string is: ". $str. "\n";echo "Is Space: ";var_dump(ctype_space($str));echo "---------\n";}?>
In the code above:
We use the ctype_space
method to check if the string contains only whitespace characters.
For the strings " "
and \t\n\r
, the ctype_space
method returns true
.
But for the strings A
and a b
, the ctype_space
method returns false
because the strings contain non-whitespace characters.