473,666 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4894
$replacement = "\${1}you_code_ here\${2}\${3}" ;
$newStr = preg_replace("/(.*?)(\<\/)(.*)/", $replacement, $str)

a bit of a guess I'm afraid

"Juha Suni" <ju*******@ilmi antajat.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_he re\$2\$3";
$newStr = preg_replace("/(.*)(\<\/)(.*)$/s", $replacement, $str);

-- Rahul

"James" <jg****@blueyon der.co.uk> wrote in message news:<mq******* **********@news-binary.blueyond er.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*******@ilmi antajat.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_he re\$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_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

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3794
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 file: <?php /* recipients */ $to = "valid@email.add" . ", " ; // note the comma $to .= $_POST;
3
7799
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. I could probably use a combination of explode, implode and substr, but I was wondering if there is a more elegant way of doing it using regular expressions?
2
2866
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 string tests $qs = 'var2=two&var1=one&var3=three'; // start $qs = 'var1=one&var2=two&var3=three'; // middle
3
5321
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 --- Sig line1 Sig line2 Sig line3 If I could just get rid of that, it would be pretty good. But I also get this
5
2207
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
2111
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 would be less of a burden for the user's browser to download. I guess the final code output by this hypothetical program could maybe even just be on one long line unless that would create problems. Maybe there is some kind of optimal line...
0
1948
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 of string manipulation where they seek through the string back and forth doing replacements to substitute in the needed HTML code. I am convinced that this can be done with a few regular expressions. Unfortunately my knowledge of regular...
1
2681
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 matches the search keyword, the problem is, it also highlight the text inside the HTML tags that matches the keywords. here's the screenshot: http://www.fileden.com/files/2006/6/11/61581/screen.JPG I need a preg_replace() function that will not...
3
1883
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 to understand all.. BUT I am here you guys can read and help me.. From this script i would need your help, You will have to separate 2 things <PLEASE> 1. Bunch of code, Which writes message and gives a unique id to each entry...SO identified...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.