Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I check a string for white space in php? 3

Status
Not open for further replies.

farley99

MIS
Feb 12, 2003
413
US
how do I check for a string for white space in php?

$str="hello";
$str2="hello man";

if(hasWhiteSpace($str))
echo "str be whitey";

if(hasWhiteSpace($str2))
echo "str2 be whitey";

is there a function for hasWhiteSpace?
 
Keep in mind, however, that if you use strpos(), you have to invoke the function multiple times, once for each character that constitutes whitespace: space, tab, newline, return, formfeed.

preg_match() will match all of those in one go.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
m98marba
What kind of overhead do you refer to? I see no mention of overhead in the PHP documentation. The only thing I see refered to in connection with strpos() is strstr() which is more memory intensive.
Please share your knowledge.
Thanks.
 
DRJ478
I havn't done any benchmarking to compare strpos() and preg_match() myself. And I don't know exactly how the (quite sofisticated) matching machinery is coded.

But, simply speaking, because preg_match() is made to handle such a variety of patterns (see you may realize that it will in most cases take more CPU-power compared to, i.e., a function like strpos(). The only thing strpos($haystack, $needle) do is finding the first occurance of $needle in $haystack so it is actually designed for that particular task so it should be faster. Moreover, I think it's mentioned somewhere in the manual (not sure can't find it) and that what I learnd myself from other programmers.

With overhead I just meant "machinery" to handle the regular expression patterns. Sorry if I was a bit unclear.

/Markus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top