recognizing and reading a new line character

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpnewbie26
    New Member
    • Jun 2009
    • 52

    #1

    recognizing and reading a new line character

    In my program I have an if else statement where I'm trying to see if that line is a skipped line. My text is like this:

    fruits

    vegetables.

    So it has to be able to know to skip to the next line if theres no text in that line.

    Code:
    if ($invalidcheck[0]=='\r\n'){
    		$i++;
    		}
    but it doesn't seem to be reading it. Does anyone know how I can fix this? Also I was wondering if it is reasonable to try to do something like if the character isn't a-z, A-Z, and all symbols.
    Thanks
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Try using the trim and empty functions to check the value.
    [code=php]<?php
    $value = "\r\n ";

    $value = trim($value);
    if(empty($value )){
    echo "Empty";
    }
    else {
    echo "Not empty";
    }
    ?>[/code]

    Originally posted by phpnewbie26
    Also I was wondering if it is reasonable to try to do something like if the character isn't a-z, A-Z, and all symbols.
    Thanks
    Whether it is reasonable to do, that's up to you, and whoever else is designing your application.

    If it is, you can easily do that using regular expressions.
    Like:
    [code=php]if(preg_match('/^[a-zA-Z]+$/i', $value))[/code]
    This would only allow the $value to contain letters.
    (And only letters... no numbers, spaces, question marks, etc...)

    Comment

    • phpnewbie26
      New Member
      • Jun 2009
      • 52

      #3
      The trim and empty function worked perfectly. Thanks! I'm new to php so I wasn't even aware of that function.=)

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by Atli
        Try using the trim and empty functions to check the value.
        [code=php]<?php
        $value = "\r\n ";

        $value = trim($value);
        if(empty($value )){
        echo "Empty";
        }
        else {
        echo "Not empty";
        }
        ?>[/code]


        Whether it is reasonable to do, that's up to you, and whoever else is designing your application.

        If it is, you can easily do that using regular expressions.
        Like:
        [code=php]if(preg_match('/^[a-zA-Z]+$/i', $value))[/code]
        This would only allow the $value to contain letters.
        (And only letters... no numbers, spaces, question marks, etc...)
        Shouldn't that be [^a-zA-Z0-9]? Or do they both work the same?

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by phpnewbie26
          The trim and empty function worked perfectly. Thanks! I'm new to php so I wasn't even aware of that function.=)
          You should try and use other options, instead of the preg_* functions as they're said to be pretty slow (the preg_* functions), although it's probably such a small difference it's pointless bringing it up (will have to benchmark it).

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by Markus
            Shouldn't that be [^a-zA-Z0-9]? Or do they both work the same?
            Just different varieties.

            Mine was like:
            [code=php]
            if(<only allowed chars found>) {
            // Do stuff
            }
            else {
            // Don't do stuff
            }[/code]
            But yours is more like:
            [code=php]
            if(<illegal chars found>) {
            // Don't do stuff
            }
            else {
            // Do stuff
            }[/code]
            Whitelist vs. Blacklist approaches... although in this case, with the ^ in the regex class, they are not so different.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by phpnewbie26
              The trim and empty function worked perfectly. Thanks! I'm new to php so I wasn't even aware of that function.=)
              Glad it worked :)

              Comment

              Working...