How can I check if a PHP string contains any white space? I want to check if white space is in there, and then echo back an error message if true
if(strlen($username) == whitespace ){ echo "<center>Your username must not contain any whitespace</center>";
PHP | ctype_space() Function A ctype_space() function in PHP is used to check whether each and every character of a string is whitespace character or not. It returns True if the all characters are white space, else returns False.
Use the test() method to check if a string contains whitespace, e.g. /\s/. test(str) . The test method will return true if the string contains at least one whitespace character and false otherwise.
Whitespaces are generally ignored in PHP syntax, but there are several places where you cannot put them without affecting the sense (and the result).
The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.
if ( preg_match('/\s/',$username) ) ....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With