Hi guys! Got stuck with this..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kulet

    Hi guys! Got stuck with this..

    "2nd post as i noone could do it"

    Hi guys! Got stuck with this..
    I have a chunks of text separated with blanc line. All I need is to
    take every last 3 lines of that text before the blanc line and put it
    in to the new text file: example:

    1some text
    2some text
    3some text
    4some text
    5some text


    6some text
    7some text
    8some text
    9some text
    10some text


    11some text
    12some text
    13some text
    14some text
    15some text


    End result mast be...


    3some text
    4some text
    5some text
    8some text
    9some text
    10some text
    13some text
    14some text
    15some text


    Any idea?

  • mootmail-googlegroups@yahoo.com

    #2
    Re: Hi guys! Got stuck with this..

    kulet wrote:
    Hi guys! Got stuck with this..
    I have a chunks of text separated with blanc line. All I need is to
    take every last 3 lines of that text before the blanc line and put it
    in to the new text file: example:

    You could keep a rolling buffer of the previous 3 lines as variables
    (or an array, which I would personally use). Each time you get a new
    line, move each previous entry down the buffer getting rid of the
    oldest. Then, when you encounter a blank line, dump your buffer to the
    file.

    Something like (untested):
    $currLine = //get line data somehow
    if (strlen($currLi ne)==0){
    //output array contents to file
    unset($buffer);
    } else {
    $buffer[] = $currLine;
    if (count($buffer) >3){
    array_shift($bu ffer);
    }
    }

    Comment

    • frothpoker

      #3
      Re: Hi guys! Got stuck with this..

      Messy but effective:-

      Read the whole of the text into a variable.

      Reverse the string. I dont know if there is a function to do this or
      whether you would have to write one.

      copy the first three lines after each paragraph to a new variable.

      Reverse this string to get it back into the correct direction.

      OR:

      more effective.

      Copy the whole text to a variable
      Split it at CRLF into an array.
      The blank lines should create an 'empty' array element
      For each array element, check the next three elements and if any of
      them are empty (not sure if it will be null or just length zero) then
      add it to a variable.

      return the variable.

      easy.

      Aaron
      mootmail-googlegroups@ya hoo.com wrote:
      kulet wrote:
      Hi guys! Got stuck with this..
      I have a chunks of text separated with blanc line. All I need is to
      take every last 3 lines of that text before the blanc line and put it
      in to the new text file: example:
      >
      >
      You could keep a rolling buffer of the previous 3 lines as variables
      (or an array, which I would personally use). Each time you get a new
      line, move each previous entry down the buffer getting rid of the
      oldest. Then, when you encounter a blank line, dump your buffer to the
      file.
      >
      Something like (untested):
      $currLine = //get line data somehow
      if (strlen($currLi ne)==0){
      //output array contents to file
      unset($buffer);
      } else {
      $buffer[] = $currLine;
      if (count($buffer) >3){
      array_shift($bu ffer);
      }
      }

      Comment

      Working...