Any idea how to do this?

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

    Any idea how to do this?

    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?

  • Erwin Moller

    #2
    Re: Any idea how to do 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:
    >
    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?
    Hi,

    That is easy. You must replace all double newlines with 1 newline.
    But beware: you say 'blanc line', but you must first find out how that is
    coded in your file.

    Often a newline is \n but sometimes it is \n\f or something like that.
    (\f for linefeed)
    It is also implemented differently on Unix, Windows, and Mac, just to make
    your life misserable.

    So you need something like this:
    $newLineOnMySys tem = "\n";
    $yourString = "sometextsomete xtetcetc";

    // Now replace all double occurences with single ones.
    $yourString = str_replace($ne wLineOnMySystem .$newLineOnMySy stem ,
    $newLineOnMySys tem, $yourString);

    Read more here about string replacement:
    Replace all occurrences of the search string with the replacement string


    But first find out what character(s) define newline on your system.

    If uncertain try this:
    You can use the function ord() to get the ascii value of a character.
    Just print them out once for all characters in your file, and see what your
    newline is.
    More here:
    Convert the first byte of a string to a value between 0 and 255



    Regards,
    Erwin Moller

    Comment

    • kulet

      #3
      Re: Any idea how to do this?

      Yes, But how do i get those last 3 lines before the blanc line???

      Comment

      • Erwin Moller

        #4
        Re: Any idea how to do this?

        kulet wrote:
        Yes, But how do i get those last 3 lines before the blanc line???
        Oh, I assumed, without any valid reason, that you only had 2 newlines.

        The poor mans solution would be to do both:
        \n\n\n -\n
        and
        \n\n -\n

        But then again you come here and ask how to do 4 lines, right? ;-)

        That can be solved easily by doing something more smarter, eg write a simple
        program that scans for repetive \n and replace them with one \n, only using
        basic stringfunctions .

        But much easier would be to use a regular expression that can recognize any
        number of repeated \n.

        Use a function like preg_replace(), read more here:


        Regards,
        Erwin Moller

        Comment

        Working...