By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

is_blank()?

P: n/a

How could I check whether a string variable contains nothing but space?
empty() does not work unless the string variable is "".

--
iTech Consulting Services Limited
Expert in ePOS (Point-Of-Sales) solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
May 11 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Man-wai Chang wrote:
>
How could I check whether a string variable contains nothing but space?
empty() does not work unless the string variable is "".
empty(trim($variable));

Hendri Kurniawan
May 11 '07 #2

P: n/a
On May 10, 10:38 pm, Hendri Kurniawan <ask...@email.comwrote:
Man-wai Chang wrote:
How could I check whether a string variable contains nothing but space?
empty() does not work unless the string variable is "".

empty(trim($variable));

Hendri Kurniawan
Actually, that will return false if the string is ' 0 ' because the
number 0 is considered emptyness by empty(). The right way to do it
is:

if( trim( $variable ) == '' )

empty() will also make sure the variable is set for you. You don't
have this luxury since you are using trim() and an operator, so you
may have to do this instead:

if( !isset( $variable ) || trim( $variable ) == '' )

....if you are validating input.

-Mike PII

May 11 '07 #3

P: n/a
Mike P2 wrote:
On May 10, 10:38 pm, Hendri Kurniawan <ask...@email.comwrote:
>Man-wai Chang wrote:
>>How could I check whether a string variable contains nothing but space?
empty() does not work unless the string variable is "".
empty(trim($variable));

Hendri Kurniawan

Actually, that will return false if the string is ' 0 ' because the
number 0 is considered emptyness by empty(). The right way to do it
is:

if( trim( $variable ) == '' )

empty() will also make sure the variable is set for you. You don't
have this luxury since you are using trim() and an operator, so you
may have to do this instead:

if( !isset( $variable ) || trim( $variable ) == '' )

...if you are validating input.

-Mike PII
Yes true :)

Hendri Kurniawan
May 11 '07 #4

P: n/a
On May 10, 10:32 pm, Man-wai Chang <toylet.toy...@gmail.comwrote:
How could I check whether a string variable contains nothing but space?
empty() does not work unless the string variable is "".

--
iTech Consulting Services Limited
Expert in ePOS (Point-Of-Sales) solutions
Website:http://www.itech.com.hk(IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
!strlen(trim($someString))

May 11 '07 #5

P: n/a
if( trim( $variable ) == '' )
if( !isset( $variable ) || trim( $variable ) == '' )
I love Foxpro's empty(). Thanks.
--
iTech Consulting Services Limited
Expert in ePOS (Point-Of-Sales) solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
May 11 '07 #6

P: n/a
"Man-wai Chang" <to***********@gmail.comwrote in message
news:46********@127.0.0.1...
>
How could I check whether a string variable contains nothing but space?
empty() does not work unless the string variable is "".

preg_match('#^ +$#', $var)

--
Ra*********@gmail.com

"Good tea. Nice house." -- Worf
May 11 '07 #7

P: n/a
Rami Elomaa wrote:
preg_match('#^ +$#', $var)
As I read the question, preg_match('#^ *$#', $var) might be better.

If tabs and other whitespace characters are to be allowed, then
preg_match('#^\s*$#', $var) even better!

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 11 '07 #8

P: n/a
Toby A Inkster kirjoitti:
Rami Elomaa wrote:
>preg_match('#^ +$#', $var)

As I read the question, preg_match('#^ *$#', $var) might be better.
Okay, my bad. I thought "nothing but spaces" as "at least one space" but
I suppose no spaces at all might also be included in the case, in which
case the * is better than + like you said.
If tabs and other whitespace characters are to be allowed, then
preg_match('#^\s*$#', $var) even better!
Yes, that would be the best choice. It's good you mentioned this as
well, perhaps the OP needs to concider this too. :)

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 11 '07 #9

P: n/a
I don't understand what's wrong with:

if( trim( $variable ) == '' )

I just benchmarked it, it's faster than the regex methods.

By the way, I think the best regex to search with would be '#\S#',
because that would indicate that the string contains at least one non-
whitespace character anywhere in the string. That is the pattern I
used in the benchmarking.

<?php //quick benchmark test
$t1 = 'asdfasdf ';
$t2 = ' ';
$i = 0;
$tTrim = microtime( true );
for( ; $i < 10000; ++$i )
{
trim( $t1 ) == '';
trim( $t2 ) == '';
}
$tTrim = microtime( true ) - $tTrim;
$i = 0;
$tRegEx = microtime( true );
for( ; $i < 10000; ++$i )
{
preg_match( '#\S#', $t1 );
preg_match( '#\S#', $t2 );
}
$tRegEx = microtime( true ) - $tRegEx;
printf( "trim: %f<br />regex: %f", $tTrim, $tRegEx );
?>

Besides, although I usually place little value in this, I think it's
easier to read ( trim( $var ) == '' ) than to interpret a regex
pattern.

-Mike PII

May 11 '07 #10

P: n/a
Mike P2 kirjoitti:
I don't understand what's wrong with:

if( trim( $variable ) == '' )
In php TIMTOWTDI (There Is More Than One Way To Do It). It's good to
know all the alternative ways of doing something and then choose the one
that best suits your needs.
I just benchmarked it, it's faster than the regex methods.
With 100000 iterations using your (slightly modified) test script I also
got results that would indicate trim is actually faster, and it's no
wonder. Trim is a single purpouse function while a regex function has
far more functionality. In this case, since trim does exactly what needs
to be accomplished, it should be faster.

Now then, the results I got. For trim and 100000 iterations, I got ~ 0.4
secs, while the regex with the same amount of iterations took ~0.8 secs,
approximately twice as long. Just for fun, I also tested with much
longer strings, using these as input:

$t1 = str_repeat(' ',1000) . 'asdfasdf';
$t2 = str_repeat(' ',1000);

The result with 10000 iterations and /^\s*$/:
trim: 0.107841
regex: 3.455793

The longer the string is, the slower the regexp gets compared to trim.
It would seem that preg_match is O(n) while trim is O(n).

Benchmarking is a good way to compare one way to another and I must
admit that trim in this case turned out faster. As to other qualities,
readability is one, but also, can it be modified? If the purpouse is to
test that user has given meaningful input, testing for spaces might not
be enough, there are other unmeaningful characters as well, such as
+-,.?!"# etc, which trim will pass... Without knowing what the OP
actually needs this test for, I can't decide which is better, but if it
is only for testing that a string contains nothing but whitespace, then
in that case trim might be optimal.

--
Ra*********@gmail.com

"Wikipedia on vhn niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tied mik pit paikkansa." -- z00ze
May 12 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.