How do I find out if a string contains ONLY whitespaces and nothing else? The number of such spaces does not matter.
if () // $string contains ONLY whitespaces and nothing else
{
// do something
}
Best if can provide a solution that can be generalised to any character: How do I find if a string contains ONLY some character and nothing else
A non-regex approach could be:
if(strlen($string) >= 1 && empty(trim($string))) {
empty(trim($string)) removes all whitespace then checks if the string is empty. strlen($string) >= 1 confirms the string wasn't empty to start with. If empty strings are also allowed that check can be removed.
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