-2

For example;

"Real Madrid" -> false
"Barcelona  " -> true

with what functions can I solve this in laravel?

Adele
  • 3
  • 1

2 Answers2

-1

A string is a single word if it:

  1. contains at least one character
  2. 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);
}
Simon Brahan
  • 2,016
  • 1
  • 14
  • 22
-1

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

Florent Cardot
  • 1,400
  • 1
  • 10
  • 19