PHP – detect whitespace between strings

Issue

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)

Answered By – hobodave

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published