For example;
"Real Madrid" -> false
"Barcelona " -> true
with what functions can I solve this in laravel?
CodePudding user response:
A string is a single word if it:
- contains at least one character
- contains no whitespace
function isSingleWord(string $input): bool
{
// Check for empty string - "" isn't a word
if (empty($input)) {
return false;
}
// Check for whitespace of any kind
return !preg_match('/\s/', $input);
}
CodePudding user response:
you can use
$result = explode(' ', trim("barcelona "));
print_r($result);
trim will remove all white space at the beginning and the end of your string