Check if string is just white space?

Check if string is just white space?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Checking if a String Is Exclusively Whitespace

Determining whether a string consists entirely of whitespace characters requires a specific approach that differs from simply checking for the presence of spaces. Many developers instinctively reach for str_contains() or regex patterns like / / to verify if a space exists within the text. However, those methods fail when the input is empty or contains only non-breaking spaces ( , \u00A0) and tabs. The goal here is stricter: we need a boolean result that returns true only if every single character in the string matches the definition of whitespace, or if the string is empty.

In PHP, the most robust built-in function for this scenario is ctype_space(). However, there is a significant caveat with this specific function. According to the PHP documentation and source behavior, ctype_space() returns false if the string is empty. This creates a logical edge case where an empty string should arguably be treated as "only whitespace" (since it contains zero non-whitespace characters), but the function treats it as containing no spaces at all.

To handle this correctly without modifying the original string, you must combine a length check with ctype_space(). A concise pattern looks like this:

function isOnlyWhitespace(string $string): bool {
    return empty($string) || ctype_space($string);
}

This logic explicitly handles the two valid states for an "only whitespace" string:

  1. Empty String: empty($string) returns true, so the function correctly identifies it as passing the check.
  2. Whitespace Only: If the string has length but ctype_space() returns true, the condition holds.

If you prefer a regular expression approach, look for patterns that assert the start and end of the string match whitespace characters. A standard regex pattern for this would be /^\s*$/. The ^ asserts the start, \s* matches zero or more whitespace characters (including spaces, tabs, newlines, and carriage returns), and $ asserts the end.

function isOnlyWhitespaceRegex(string $string): bool {
    return preg_match('/^\s*$/', $string) === 1;
}

While regex offers flexibility for complex character classes (like including specific Unicode whitespace), ctype_space() is generally faster because it operates at the C level and does not involve pattern compilation overhead. For high-performance applications processing millions of records, avoiding unnecessary regex execution is a worthwhile optimization.

Handling Unicode Whitespace

One common pitfall in modern development involves internationalization. The standard \s in PCRE (Perl-Compatible Regular Expressions) matches Unicode whitespace by default when using the u modifier or in PHP 7+ without explicit flags depending on locale settings, but ctype_space() is locale-dependent. In a "C" locale, it only matches ASCII spaces and tabs.

If your application supports UTF-8 content, relying solely on ctype_space() might incorrectly return false for strings containing non-breaking spaces or other Unicode whitespace characters. To ensure correctness across all environments regardless of the server's default locale, you should set the locale to "C" before using ctype_space(), or stick to the regex approach which handles Unicode whitespace more predictably in modern PHP versions.

// Safer for mixed locales without manual locale setting
return preg_match('/^\s*$/', $string) === 1;

Performance Considerations

When writing this check, avoid using trim() first to determine the length. While strlen(trim($string)) === 0 is a popular one-liner, it creates a new string in memory. If you are processing large payloads or working within tight memory constraints (such as streaming data), creating a copy of the string just to measure its trimmed length introduces unnecessary overhead. The methods shown above inspect the original buffer without modification, preserving memory efficiency.

Ultimately, the choice between ctype_space() and regex depends on your specific needs regarding speed versus character set complexity. For simple ASCII-only inputs, the combined empty check with ctype_space() is efficient. For UTF-8 heavy applications, the regex pattern /^\s*$/ provides a consistent guarantee that no non-whitespace characters exist within the bounds of the string.

Our network