Search code examples
phpregexwhitespace

PHP - detect whitespace between strings


How would I go about detecting whitespace between strings? For example, I have a name string like:

"Jane Doe"

Keep in mind that I don't want to trim or replace it, just detect if whitespace exists between the first and second string.


Solution

  • Use preg_match as suggested by Josh:

    <?php
    
    $foo = 'Bob Williams';
    $bar = 'SamSpade';
    $baz = "Bob\t\t\tWilliams";
    
    var_dump(preg_match('/\s/',$foo));
    var_dump(preg_match('/\s/',$bar));
    var_dump(preg_match('/\s/',$baz));
    

    Ouputs:

    int(1)
    int(0)
    int(1)