Splitting paragraphs in a text.

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

    #1

    Splitting paragraphs in a text.

    I am splitting a text block into paragraphs, to be able to add images and stuff
    like that to a specific paragraph in a content management system.

    Well, right now I'm splittin on two or more newlines, so this text block:

    Hello, my nickname is Sandman and I am coding
    some PHP

    Call me

    Would be split into two parts, with "Call me" being the second one.

    My problem now is that if I have a text block like below:

    Hello, my nickname is Sandman and I am coding
    some PHP. Here is an example:

    <code>
    print "Hello World!";

    print "Foo";
    </code>

    Call me

    The above would, given the rules I use now, yield four parts, as such:

    ---------------------------------------------
    Hello, my nickname is Sandman and I am coding
    some PHP. Here is an example:
    ---------------------------------------------
    <code>
    print "Hello World!";
    ---------------------------------------------
    print "Foo";
    </code>
    ---------------------------------------------
    Call me
    ---------------------------------------------

    But I would want it to end up in three parts, as such:

    ---------------------------------------------
    Hello, my nickname is Sandman and I am coding
    some PHP. Here is an example:
    ---------------------------------------------
    <code>
    print "Hello World!";

    print "Foo";
    </code>
    ---------------------------------------------
    Call me
    ---------------------------------------------

    So, basically, what I want to do is to split the text block up with the
    delimiter "\n{2,}" but not when it is inside an *unclosed* html tag. Some
    examples:


    <div class='quote'>
    Hello, my nickname is Sandman and I am coding
    some PHP. Here is an example:

    <code>
    print "Hello World!";

    print "Foo";
    </code>

    Call me
    </div>

    Ends up in:

    ---------------------------------------------
    <div class='quote'>
    Hello, my nickname is Sandman and I am coding
    some PHP. Here is an example:

    <code>
    print "Hello World!";

    print "Foo";
    </code>

    Call me
    </div>
    ---------------------------------------------

    And

    <div class='quote'>
    Hello, my nickname is Sandman and I am coding
    some PHP. Here is an example:

    <code>
    print "Hello World!";

    print "Foo";
    </code>
    </div>

    Call me

    Ends up in:

    ---------------------------------------------
    <div class='quote'>
    Hello, my nickname is Sandman and I am coding
    some PHP. Here is an example:

    <code>
    print "Hello World!";

    print "Foo";
    </code>
    </div>
    ---------------------------------------------
    Call me
    ---------------------------------------------


    Hopefully you get the idea.

    Any ideas on how to solve it?

    --
    Sandman[.net]
  • chotiwallah

    #2
    Re: Splitting paragraphs in a text.

    Sandman <mr@sandman.net > wrote in message news:<mr-4DACD5.11445712 082004@individu al.net>...[color=blue]
    > I am splitting a text block into paragraphs, to be able to add images and stuff
    > like that to a specific paragraph in a content management system.
    >
    > Well, right now I'm splittin on two or more newlines, so this text block:
    >
    > Hello, my nickname is Sandman and I am coding
    > some PHP
    >
    > Call me
    >
    > Would be split into two parts, with "Call me" being the second one.
    >
    > My problem now is that if I have a text block like below:
    >
    > Hello, my nickname is Sandman and I am coding
    > some PHP. Here is an example:
    >
    > <code>
    > print "Hello World!";
    >
    > print "Foo";
    > </code>
    >
    > Call me
    >
    > The above would, given the rules I use now, yield four parts, as such:
    >
    > ---------------------------------------------
    > Hello, my nickname is Sandman and I am coding
    > some PHP. Here is an example:
    > ---------------------------------------------
    > <code>
    > print "Hello World!";
    > ---------------------------------------------
    > print "Foo";
    > </code>
    > ---------------------------------------------
    > Call me
    > ---------------------------------------------
    >
    > But I would want it to end up in three parts, as such:
    >
    > ---------------------------------------------
    > Hello, my nickname is Sandman and I am coding
    > some PHP. Here is an example:
    > ---------------------------------------------
    > <code>
    > print "Hello World!";
    >
    > print "Foo";
    > </code>
    > ---------------------------------------------
    > Call me
    > ---------------------------------------------
    >
    > So, basically, what I want to do is to split the text block up with the
    > delimiter "\n{2,}" but not when it is inside an *unclosed* html tag. Some
    > examples:
    >
    >
    > <div class='quote'>
    > Hello, my nickname is Sandman and I am coding
    > some PHP. Here is an example:
    >
    > <code>
    > print "Hello World!";
    >
    > print "Foo";
    > </code>
    >
    > Call me
    > </div>
    >
    > Ends up in:
    >
    > ---------------------------------------------
    > <div class='quote'>
    > Hello, my nickname is Sandman and I am coding
    > some PHP. Here is an example:
    >
    > <code>
    > print "Hello World!";
    >
    > print "Foo";
    > </code>
    >
    > Call me
    > </div>
    > ---------------------------------------------
    >
    > And
    >
    > <div class='quote'>
    > Hello, my nickname is Sandman and I am coding
    > some PHP. Here is an example:
    >
    > <code>
    > print "Hello World!";
    >
    > print "Foo";
    > </code>
    > </div>
    >
    > Call me
    >
    > Ends up in:
    >
    > ---------------------------------------------
    > <div class='quote'>
    > Hello, my nickname is Sandman and I am coding
    > some PHP. Here is an example:
    >
    > <code>
    > print "Hello World!";
    >
    > print "Foo";
    > </code>
    > </div>
    > ---------------------------------------------
    > Call me
    > ---------------------------------------------
    >
    >
    > Hopefully you get the idea.
    >
    > Any ideas on how to solve it?[/color]

    the easiest way would be using empty lines only where you want a
    split. if you want empty lines inside tags because of readability you
    could simply mask them by i. e. putting in an empty comment. this
    works of course only if you can control the output of your cms to that
    degree.

    micha

    Comment

    • Sandman

      #3
      Re: Splitting paragraphs in a text.

      In article <782d6cb.040812 2207.42239b3b@p osting.google.c om>,
      chotiwallah@web .de (chotiwallah) wrote:
      [color=blue]
      > the easiest way would be using empty lines only where you want a
      > split. if you want empty lines inside tags because of readability you
      > could simply mask them by i. e. putting in an empty comment. this
      > works of course only if you can control the output of your cms to that
      > degree.[/color]

      But I can't. Thousands of people are writing stuff into the CMS, and I can't
      tell them to first replace newlines with character X when they cut'n'paste into
      the CMS.

      --
      Sandman[.net]

      Comment

      • Sandman

        #4
        Solved! (was: Re: Splitting paragraphs in a text.)

        In article <mr-4DACD5.11445712 082004@individu al.net>, I wrote about splitting a
        block of text into an array, but being careful about nested tags.

        I have now solved all by myself, and thought I'd might share it (so everyone in
        the future won't find an empty thread on google :)

        Here is a working example:

        #!/usr/bin/php
        <?
        $text=<<<END
        Hello, my nickname is Sandman, and I like PHP, some examples:

        <code>
        print "Hello World";

        print "Foobar";
        </code>

        Here are nested tags:

        <quote>
        <quote>
        He said he liked flowers
        </quote>

        Well, he doesn't, ok.

        <quote>I like them</quote>

        Good for you
        </quote>

        <div class="paragrap h">
        Nice paragraph
        </div>

        <img src="foo.jpg"> <- Nice pic!
        END;

        $inside = 0; # we begin "outside"
        $paragraphs = array();

        # no multiple newlines, 2 is max - which means there is a new paragraph
        $text = preg_replace("/\n{2,}/", "\n\n", $text);

        # These are the container tags we wan't to spare.
        $cont="(quote|d iv|ul|ol|code|p re)";

        foreach (split("\n", "$text\n") as $line){
        if (preg_match("#< $cont(.*?)>#", $line, $m)){
        if (!preg_match("# </$m[1]>#", $line)){
        # at this point, we've found the beginning tag
        # but not the ending tag, so we're definately inside
        $inside++;
        }
        }
        if (preg_match("#</$cont>#", $line, $m)){
        if (!preg_match("# <$m[1](.*?)>#", $line)){
        # Aha, we have no found an ending tag, but not the tag
        # that started it, which means we've stepped out one step
        $inside--;
        }
        }
        if (($line == "") && ($inside == 0) && ($agg)){
        # Ok, we've reached a "\n\n" place in the textblock
        # and we're not inside anything, and we have aggregated
        # some text.
        $paragraphs[] = $agg;
        $agg=""; # reset the aggregation.
        continue; # don't process the empty line.
        }
        $agg.="$line\n" ; # Aggregate everything.
        print "$inside: $line\n"; # Debugging!
        }

        foreach ($paragraphs as $p){
        print "---------------\n";
        print "$p";
        }
        ?>

        And the output is:

        0: Hello, my nickname is Sandman, and I like PHP, some examples:
        1: <code>
        1: print "Hello World";
        1:
        1: print "Foobar";
        0: </code>
        0: Here are nested tags:
        1: <quote>
        2: <quote>
        2: He said he liked flowers
        1: </quote>
        1:
        1: Well, he doesn't, ok.
        1:
        1: <quote>I like them</quote>
        1:
        1: Good for you
        0: </quote>
        1: <div class="paragrap h">
        1: Nice paragraph
        0: </div>
        0: <img src="foo.jpg"> <- Nice pic!
        ---------------
        Hello, my nickname is Sandman, and I like PHP, some examples:
        ---------------
        <code>
        print "Hello World";

        print "Foobar";
        </code>
        ---------------
        Here are nested tags:
        ---------------
        <quote>
        <quote>
        He said he liked flowers
        </quote>

        Well, he doesn't, ok.

        <quote>I like them</quote>

        Good for you
        </quote>
        ---------------
        <div class="paragrap h">
        Nice paragraph
        </div>
        ---------------
        <img src="foo.jpg"> <- Nice pic!

        --
        Sandman[.net]

        Comment

        Working...