PHP - detect whitespace between strings


Question

How would I go about detecting whitespace within a string? 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.

1
30
7/21/2009 9:38:30 PM

Accepted Answer

Use preg_match as suggested by Josh:

<?php

$foo = "Dave Smith";
$bar = "SamSpade";
$baz = "Dave\t\t\tSmith";

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)
77
7/21/2009 9:07:19 PM

Wouldn't preg_match("/\s/",$string) work? The advantage to this over strpos is that it will detect any whitespace, not just spaces.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon