is_blank()?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Man-wai Chang

    is_blank()?


    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
  • Hendri Kurniawan

    #2
    Re: is_blank()?

    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($var iable));

    Hendri Kurniawan

    Comment

    • Mike P2

      #3
      Re: is_blank()?

      On May 10, 10:38 pm, Hendri Kurniawan <ask...@email.c omwrote:
      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($var iable));
      >
      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

      Comment

      • Hendri Kurniawan

        #4
        Re: is_blank()?

        Mike P2 wrote:
        On May 10, 10:38 pm, Hendri Kurniawan <ask...@email.c omwrote:
        >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($va riable));
        >>
        >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

        Comment

        • ZeldorBlat

          #5
          Re: is_blank()?

          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($s omeString))

          Comment

          • Man-wai Chang

            #6
            Re: is_blank()?

            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

            Comment

            • Rami Elomaa

              #7
              Re: is_blank()?

              "Man-wai Chang" <toylet.toylet@ gmail.comwrote in message
              news:4643d5b9$1 @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)

              --
              Rami.Elomaa@gma il.com

              "Good tea. Nice house." -- Worf


              Comment

              • Toby A Inkster

                #8
                Re: is_blank()?

                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
                Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

                Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

                Comment

                • Rami Elomaa

                  #9
                  Re: is_blank()?

                  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. :)

                  --
                  Rami.Elomaa@gma il.com

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

                  Comment

                  • Mike P2

                    #10
                    Re: is_blank()?

                    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

                    Comment

                    • Rami Elomaa

                      #11
                      Re: is_blank()?

                      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.

                      --
                      Rami.Elomaa@gma il.com

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

                      Comment

                      Working...