How to count lines from a form?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andy.z@blueyonder.com

    How to count lines from a form?


    I'm having trouble working this out ...

    Is there a simple way to count how many lines are returned in
    $_POST['myTextArea']

    When I look at the $_POST all I see is one line yet there are three.
    I assume something is being interpreted differently depending on how I
    view it.

    Is there a simple funtion for this or can someone tell me how to work it
    out please?

    Thanks

    andy
  • jon.pulice@gmail.com

    #2
    Re: How to count lines from a form?

    On Mar 12, 5:58 am, and...@blueyond er.com wrote:
    I'm having trouble working this out ...
    >
    Is there a simple way to count how many lines are returned in
    $_POST['myTextArea']
    >
    When I look at the $_POST all I see is one line yet there are three.
    I assume something is being interpreted differently depending on how I
    view it.
    >
    Is there a simple funtion for this or can someone tell me how to work it
    out please?
    >
    Thanks
    >
    andy

    Hey,

    This should do the trick,

    $lineCount = count( explode( "\n" , $_POST['test'] ) );

    where 'test' is the nameof the textarea field

    Comment

    • Richard Formby

      #3
      Re: How to count lines from a form?

      andy.z@blueyond er.com wrote:
      I'm having trouble working this out ...
      >
      Is there a simple way to count how many lines are returned in
      $_POST['myTextArea']
      >
      When I look at the $_POST all I see is one line yet there are three.
      I assume something is being interpreted differently depending on how I
      view it.
      >
      Is there a simple funtion for this or can someone tell me how to work it
      out please?
      Do you reallly have three lines in your textarea, that it three bits of text
      seperated by \n (the enter key)? Or, is your textarea simply word-wrapping
      the text onto three "visible" lines?

      --
      Richard.


      Comment

      • andy.z@blueyonder.com

        #4
        Re: How to count lines from a form?

        In article <1173696650.014 897.36950@s48g2 000cws.googlegr oups.com>,
        jon.pulice@gmai l.com says...
        On Mar 12, 5:58 am, and...@blueyond er.com wrote:
        I'm having trouble working this out ...

        Is there a simple way to count how many lines are returned in
        $_POST['myTextArea']

        When I look at the $_POST all I see is one line yet there are three.
        I assume something is being interpreted differently depending on how I
        view it.

        Is there a simple funtion for this or can someone tell me how to work it
        out please?

        Thanks

        andy
        >
        >
        Hey,
        >
        This should do the trick,
        >
        $lineCount = count( explode( "\n" , $_POST['test'] ) );
        >
        where 'test' is the nameof the textarea field
        >
        >
        clever!
        Thanks - that makes sense now I've seen it ;-)
        I'll give it a go.

        cheers

        andy

        Comment

        • Tom

          #5
          Re: How to count lines from a form?

          On Mar 12, 8:25 am, and...@blueyond er.com wrote:
          In article <1173696650.014 897.36...@s48g2 000cws.googlegr oups.com>,
          jon.pul...@gmai l.com says...
          >
          >
          >
          >
          >
          On Mar 12, 5:58 am, and...@blueyond er.com wrote:
          I'm having trouble working this out ...
          >
          Is there a simple way to count how many lines are returned in
          $_POST['myTextArea']
          >
          When I look at the $_POST all I see is one line yet there are three.
          I assume something is being interpreted differently depending on how I
          view it.
          >
          Is there a simple funtion for this or can someone tell me how to work it
          out please?
          >
          Thanks
          >
          andy
          >
          Hey,
          >
          This should do the trick,
          >
          $lineCount = count( explode( "\n" , $_POST['test'] ) );
          >
          where 'test' is the nameof the textarea field
          >
          clever!
          Thanks - that makes sense now I've seen it ;-)
          I'll give it a go.
          >
          cheers
          >
          andy- Hide quoted text -
          >
          - Show quoted text -
          If you want to be precise, you should `trim` your result first to
          check to see if any value was passed. `explode` will always return an
          array of length 1, even if the string parameter is empty (or even
          NULL):

          <?php
          $string = "Hello world" ;
          echo count( explode( "\n" , $string ) ) ; // "1"
          $string = "" ;
          echo count( explode( "\n" , $string ) ) ; // "1", not sure if that's
          what you need or not
          ?>

          `trim` will also get rid of newlines at the end of a string -- I've
          noticed that on Safari in particular, textareas seem to add newlines
          regardless of where "Enter" or "Return" was hit.

          Comment

          Working...