Reg-Ex Replacer with Function

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

    Reg-Ex Replacer with Function

    I want to do the following (and I suspect the easiest way to be a
    regular expression that uses a user-function):

    Given the string...
    --------
    <p>Hello</p>

    <p class="bla">Wor ld</p>

    <h3>Hello User</h3>

    <p>How are you</p>
    --------


    I want to change it to:
    --------
    <p>Hello</p>

    <div class="annotati on">That was paragraph 1</div>

    <p class="bla">Wor ld</p>

    <div class="annotati on">That was paragraph 2</div>

    <h3>Hello User</h3>

    <p>How are you</p>

    <div class="annotati on">That was paragraph 3</div>
    --------

    So in other words, I want to add a string after every paragraph with a
    iterated number. So I suspect I can do the following:

    - Replace all "</p>" with "</p>...<div ..$n..." but increase the $n by
    1 for every time

    Can anybody tell me how to do this? Thanks!
  • Philipp Lenssen

    #2
    Re: Reg-Ex Replacer with Function

    Philipp Lenssen wrote:
    [color=blue]
    >
    > - Replace all "</p>" with "</p>...<div ..$n..." but increase the $n by
    > 1 for every time
    >[/color]

    Uhm, OK, a simple explode() did the job...

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: Reg-Ex Replacer with Function

      "Philipp Lenssen" <info@outer-court.com> wrote in message news:<bvnvfk$su lve$1@ID-203055.news.uni-berlin.de>...[color=blue]
      > Philipp Lenssen wrote:
      >[color=green]
      > >
      > > - Replace all "</p>" with "</p>...<div ..$n..." but increase the $n by
      > > 1 for every time
      > >[/color]
      >
      > Uhm, OK, a simple explode() did the job...[/color]

      Anyway, try http://www.weitz.de/regex-coach/

      --
      "Success = 10% sweat + 90% tears"
      Email: rrjanbiah-at-Y!com

      Comment

      • Chung Leong

        #4
        Re: Reg-Ex Replacer with Function

        Use preg_replace_ca llback():

        $annotations = array( ... );
        $para_counter = 0;

        function anno_callback($ match) {
        global $annotations, $para_counter;
        $annotation = htmlspecialchar s($annotations[$para_counter++]);
        return "$match\n<d iv class=\"annotat ion\">$annotati on</div>\n";
        }

        preg_replace_ca ll('|</p>|i', 'anno_callback' , $text);

        Uzytkownik "Philipp Lenssen" <info@outer-court.com> napisal w wiadomosci
        news:bvnsm1$ue0 gc$1@ID-203055.news.uni-berlin.de...[color=blue]
        > I want to do the following (and I suspect the easiest way to be a
        > regular expression that uses a user-function):
        >
        > Given the string...
        > --------
        > <p>Hello</p>
        >
        > <p class="bla">Wor ld</p>
        >
        > <h3>Hello User</h3>
        >
        > <p>How are you</p>
        > --------
        >
        >
        > I want to change it to:
        > --------
        > <p>Hello</p>
        >
        > <div class="annotati on">That was paragraph 1</div>
        >
        > <p class="bla">Wor ld</p>
        >
        > <div class="annotati on">That was paragraph 2</div>
        >
        > <h3>Hello User</h3>
        >
        > <p>How are you</p>
        >
        > <div class="annotati on">That was paragraph 3</div>
        > --------
        >
        > So in other words, I want to add a string after every paragraph with a
        > iterated number. So I suspect I can do the following:
        >
        > - Replace all "</p>" with "</p>...<div ..$n..." but increase the $n by
        > 1 for every time
        >
        > Can anybody tell me how to do this? Thanks![/color]


        Comment

        Working...