473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 17 '05 #1
3 4879
$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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rainmaker | last post by:
Greetings, I have not been able to find the documentation that will allow me to insert php code inside the html code in the $message block in the mail() function. Using the example in the help...
3
by: Ken Robinson | last post by:
This one has me stumped... Let's say I have a string consisting of n words (where n > 0). I would like to insert string 'abcd' between the first and second characters of each word of the string....
2
by: Clint Pachl | last post by:
Are backreferences inside look-aheads permitted? I am trying to remove a name=value pair from a url's query string using php's preg_replace. Here's what I am trying to accomplish: // query...
3
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
5
by: lawrence k | last post by:
I do not know much about regex. I'm worried about lines like this: <a href="myFile>my file</a> There is only one quote mark in that html. I wanted to fix this problem, so I tried this:
4
by: KenFehling | last post by:
Hello. I am wondering if there exists a piece of software that takes multiple .js files that are nicely indented and commented and create one big tightly packed .js file. I'm hoping the one file...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
1
by: countocram | last post by:
I have big problem, I'm using preg_replace() function for my highlighter function, after searching for particular keyword, once the hightler check box is checked it will highlight the content that...
3
by: Faisal Shah | last post by:
As the solution.. I have got this script code.. it's an open source so i can modify it.. The problem is it's a guest book script written in very highly and deeply php language that I am not able...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.