inserting code inside string, preg_replace help.

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

    inserting code inside string, preg_replace help.

    Hi!

    I have managed to live without using too much regular expressions so
    far, and now that I need one, I need some help too.

    I have a string containing a (possibly large) block of html. I need to
    insert code (an image to be precise) right after the last textual
    character in the string. This is, of course, no problem if the end
    contains plain text and no html. If the string ends in HTML however, I
    need to fiddle around. for example consider that the string ends like
    "... lorem ipsum.</p></blockquote>". I need to insert code between
    "ipsum." and "</p>".

    So what kind of regular expression I need, or is there another way I
    have missed, to insert code into the string, right after the last
    visible character, but before the possibly following html tags (usually
    ending tags such as the mentioned </p> and </blockquote>).

    Thank you in advance.

    --
    Suni

  • James

    #2
    Re: inserting code inside string, preg_replace help.

    $replacement = "\${1}you_code_ here\${2}\${3}" ;
    $newStr = preg_replace("/(.*?)(\<\/)(.*)/", $replacement, $str)

    a bit of a guess I'm afraid

    "Juha Suni" <juha.suni@ilmi antajat.fi> wrote in message
    news:3fe30c76$0 $7230$39db0f71@ news.song.fi...[color=blue]
    > Hi!
    >
    > I have managed to live without using too much regular expressions so
    > far, and now that I need one, I need some help too.
    >
    > I have a string containing a (possibly large) block of html. I need to
    > insert code (an image to be precise) right after the last textual
    > character in the string. This is, of course, no problem if the end
    > contains plain text and no html. If the string ends in HTML however, I
    > need to fiddle around. for example consider that the string ends like
    > "... lorem ipsum.</p></blockquote>". I need to insert code between
    > "ipsum." and "</p>".
    >
    > So what kind of regular expression I need, or is there another way I
    > have missed, to insert code into the string, right after the last
    > visible character, but before the possibly following html tags (usually
    > ending tags such as the mentioned </p> and </blockquote>).
    >
    > Thank you in advance.
    >
    > --
    > Suni
    >[/color]


    Comment

    • Rahul Anand

      #3
      Re: inserting code inside string, preg_replace help.

      Hi Juha, James

      I tested the regular expression written by James and found it
      incorrect. This expression inserts the repalcement code before all
      instances of <\.

      The correct one is as follows:

      $replacement = "\$1you_code_he re\$2\$3";
      $newStr = preg_replace("/(.*)(\<\/)(.*)$/s", $replacement, $str);

      -- Rahul

      "James" <jgauld@blueyon der.co.uk> wrote in message news:<mqIEb.723 5$C71.1707@news-binary.blueyond er.co.uk>...[color=blue]
      > $replacement = "\${1}you_code_ here\${2}\${3}" ;
      > $newStr = preg_replace("/(.*?)(\<\/)(.*)/", $replacement, $str)
      >
      > a bit of a guess I'm afraid
      >
      > "Juha Suni" <juha.suni@ilmi antajat.fi> wrote in message
      > news:3fe30c76$0 $7230$39db0f71@ news.song.fi...[color=green]
      > > Hi!
      > >
      > > I have managed to live without using too much regular expressions so
      > > far, and now that I need one, I need some help too.
      > >
      > > I have a string containing a (possibly large) block of html. I need to
      > > insert code (an image to be precise) right after the last textual
      > > character in the string. This is, of course, no problem if the end
      > > contains plain text and no html. If the string ends in HTML however, I
      > > need to fiddle around. for example consider that the string ends like
      > > "... lorem ipsum.</p></blockquote>". I need to insert code between
      > > "ipsum." and "</p>".
      > >
      > > So what kind of regular expression I need, or is there another way I
      > > have missed, to insert code into the string, right after the last
      > > visible character, but before the possibly following html tags (usually
      > > ending tags such as the mentioned </p> and </blockquote>).
      > >
      > > Thank you in advance.
      > >
      > > --
      > > Suni
      > >[/color][/color]

      Comment

      • Juha Suni

        #4
        Re: inserting code inside string, preg_replace help.

        Thanks for the tips.

        Rahul Anand wrote:[color=blue]
        > The correct one is as follows:
        >
        > $replacement = "\$1you_code_he re\$2\$3";
        > $newStr = preg_replace("/(.*)(\<\/)(.*)$/s", $replacement, $str);[/color]

        Actually this does not work as I wished either. This works fine if the
        textblock ends in a single closing html tag, but does not work correctly
        if there are several:

        string "..lorem ipsum dolor</p></blockquote>"
        becomes "..lorem ipsum dolor</p>you_code_here </blockquote>"
        whereas it should become "..lorem ipsum
        doloryou_code_h ere</p></blockquote>"

        So I finally dug my head deeper into regexp and found the solution:

        $replacement = 'mycodehere' . "\$0";
        $newStr = preg_replace("/(\<\/\w*\>)*$/s", $replacement, $str, 1);

        I am using the code to insert a small nice graphic image after the last
        word in the document, often seen in magazines marking the end of the
        article. The actual content comes from a CMS.

        --
        Suni

        Comment

        Working...