By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

inserting code inside string, preg_replace help.

P: n/a
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

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
$replacement = "\${1}you_code_here\${2}\${3}";
$newStr = preg_replace("/(.*?)(\<\/)(.*)/", $replacement, $str)

a bit of a guess I'm afraid

"Juha Suni" <ju*******@ilmiantajat.fi> wrote in message
news:3f**********************@news.song.fi...
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

Jul 17 '05 #2

P: n/a
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_here\$2\$3";
$newStr = preg_replace("/(.*)(\<\/)(.*)$/s", $replacement, $str);

-- Rahul

"James" <jg****@blueyonder.co.uk> wrote in message news:<mq*****************@news-binary.blueyonder.co.uk>...
$replacement = "\${1}you_code_here\${2}\${3}";
$newStr = preg_replace("/(.*?)(\<\/)(.*)/", $replacement, $str)

a bit of a guess I'm afraid

"Juha Suni" <ju*******@ilmiantajat.fi> wrote in message
news:3f**********************@news.song.fi...
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

Jul 17 '05 #3

P: n/a
Thanks for the tips.

Rahul Anand wrote:
The correct one is as follows:

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


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_here</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

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.