views:

271

answers:

5

Is there a way I can check for whitespace?

For example, I DO NOT want to check for whitespace such as this...

$string = "The dog ran away!"; and have it output Thedogranaway!

I want to check if there if the entry is ALL whitespace and whitespace only?

alt text and have it output the error!

Basically, I don't want to be able to enter all whitespace, and still have it do the mysql_query.
Also, is there a way I can strip all the whitespace before the string, and not from the rest of the string?

$string = " "WHITESPACE HERE" The dog ran away!";

if(empty($string) OR $string == " "){ // crappy example of what i'm trying to do
echo "Try again, please enter a message!"; // error
} else {
mysql_query("UPDATE users SET mesg='$string'")or die(mysql_error()); // do something
echo $post;
}
+5  A: 

How about:

if (strlen(trim($string)) == 0)

or, the possibly more efficient:

if (trim($string) == '')

or, you could use a regular expression:

if (preg_match("/^\s+$/", $string) != 0)
James McNellis
or `trim($string) == ''` might be slightly more efficient?
Mark
It might be; it depends on whether PHP stores the length of the string or if it null-terminates strings (I don't know which it uses). If it stores the length, both will perform about equally; if it uses null-terminated strings, performing a string comparison would probably be faster.
James McNellis
Well, if `strlen` performs equal or worse, it's better to go with the safer bet. Not that it really matters unless you're doing this 8 billion times.
Mark
I agree. I have to say, though, in terms of both elegance and raw performance, GZipp's answer is arguably the best of all the options presented on this page, by far.
James McNellis
+2  A: 
if ( trim($string) ) {
    // string contains text
}
else {
    // string contains only spaces or is empty
}
Galen
trim($string) evaluates to true if it is not an empty string?
jaywon
In PHP, an empty string _and_ the string `"0"` both evaluate to `false`. This fails for the latter case.
James McNellis
+1  A: 

You can use the trim() function on an empty string:

if (strlen(trim($string)) == 0){
   //do something
}else{
  // do something else
}

To trim leading white space:

ltrim($string)

In the previous example " This is text" would return "This is text". Also trim() would achieve the same result, but trim() removes whitespace before and after the string.

jaywon
+1  A: 

Maybe you are looking for a trim() method. In Java it would be String.trim(). I don't know how it should looks like in php, however this link might help http://php.net/manual/en/function.trim.php

nandokakimoto
+4  A: 

There is also ctype_space (the easiest, imo, and made to do just this, without doing a bunch of unnecessary string manipulation):

$str = "   \r\n   \t  ";
if (ctype_space($str)) {
    echo 'All whitespace';
}
// All whitespace
GZipp
+1 for introducing me to a PHP function I've never used before.
James McNellis
Very helpful function.
Homework
I'm glad. :) It seems to me that all the ctype functions are relatively unknown and underused, even though they've been there since early PHP 4.
GZipp
Ended up using this, so much easier.
Homework