473,387 Members | 1,553 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,387 software developers and data experts.

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">World</p>

<h3>Hello User</h3>

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

<div class="annotation">That was paragraph 1</div>

<p class="bla">World</p>

<div class="annotation">That was paragraph 2</div>

<h3>Hello User</h3>

<p>How are you</p>

<div class="annotation">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!
Jul 17 '05 #1
3 1705
Philipp Lenssen wrote:

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


Uhm, OK, a simple explode() did the job...
Jul 17 '05 #2
"Philipp Lenssen" <in**@outer-court.com> wrote in message news:<bv************@ID-203055.news.uni-berlin.de>...
Philipp Lenssen wrote:

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


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


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

--
"Success = 10% sweat + 90% tears"
Email: rrjanbiah-at-Y!com
Jul 17 '05 #3
Use preg_replace_callback():

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

function anno_callback($match) {
global $annotations, $para_counter;
$annotation = htmlspecialchars($annotations[$para_counter++]);
return "$match\n<div class=\"annotation\">$annotation</div>\n";
}

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

Uzytkownik "Philipp Lenssen" <in**@outer-court.com> napisal w wiadomosci
news:bv************@ID-203055.news.uni-berlin.de...
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">World</p>

<h3>Hello User</h3>

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

<div class="annotation">That was paragraph 1</div>

<p class="bla">World</p>

<div class="annotation">That was paragraph 2</div>

<h3>Hello User</h3>

<p>How are you</p>

<div class="annotation">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!

Jul 17 '05 #4

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

Similar topics

0
by: Sebastian | last post by:
Hi all, Does anyone know if there's a way in .net to read a registry file? I mean a registry file (.reg file), not the local registry. I need to work with registry info from another machine and do...
11
by: Mark J. McGinty | last post by:
Greets, One site I've written allows the user to install an IE extension menu (not malware at all) by downloading/merging a short .REG file. Worked like a peach on Win2K Server, now that I...
5
by: Henok Girma | last post by:
Hello Gurus, I wanted to have a very simple, strict regular expression validator for a phone field. the only valid format is (XXX) XXX-XXXX where X is a digit I have the following but it always...
1
by: dixie | last post by:
I have a .reg file that I need to run across a network. Currently, we run it for each logon on the network manually, which is not really working well as there are over a hundred users and they...
5
by: Marty McDonald | last post by:
I'm trying to manually edit the resistry so Asp.Net can write to the app log. If my web app is named "TestApp", wouldn't I just create a key called "TestApp" under...
5
by: Edwin Knoppert | last post by:
I have registered and i expected some reg code, not! The balloon keeps appearing, how to get rid of that? vwd-express
7
by: DraguVaso | last post by:
Hi, Does anybody knows how to run a *.reg-file silently on a computer? I need this to do automatic updates of my application without user-interaction. So I don't want the user to be asked a...
1
by: Anurag | last post by:
Hi, I have 2 related questions. DB2 UDB ESE v8.x (8.1 till 8.2 FP2 - all fixpaks included) on AIX 5.4.x _____________________________________________________________________________ (QUESTION 1)...
1
by: black_13 | last post by:
are there any python modules for manipulation of .reg files producted by the win32 prog "reg". thanks. black_13
6
by: LeWalrus | last post by:
Hi, I've written a reg exp for capturing a group of numbers from text files in the following format: -1.4326 s < 0.6758 s < 1.4334 s Any of the numbers can be positive or negative and the units...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.