473,324 Members | 2,313 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,324 software developers and data experts.

Why is preg_replace replacing only last occurance of target in string?

I am developing an authoring system that includes footnote functionality. The system stores data as htmlspecialchars encoded html. Footnotes are numbered in the source without regard to where the footnote appears in the text. So, footnote 5 could preceed footnote 1 in the html. When its time to process the document for presentation I am re-assigning the footnote numbers so that they appear sequentially in the text. I am trying to do this using preg_replace.

Each footnote is embedded in a <span> that has an ID attribute with a unique identifier that maps the corresponding footnote text to the given marker location. The following code will reproduce the problem:

Expand|Select|Wrap|Line Numbers
  1. error_reporting(E_ALL);
  2. $regexp = '/^(.*)<span (.*)id="fn([^"]*)"(.*)>\[(.*)\](.*)$/';
  3. $target = <<<__EOS
  4. <b>MANAGEMENT'S DISCUSSION AND ANALYSIS</b> <p>This text is shorter.  <sup><span style="color: blue; font-size: 10px;" id="fn6" class="footnote">[6]</span></sup>.&nbsp; There has to be at least two sentences presented <sup><span style="color: blue; font-size: 10px;" id="fn4" class="footnote">[4]</span></sup>) discussion and analysis is designed to identify the significant <sup><span style="color: blue; font-size: 10px;" id="fn5" class="footnote">[5]</span></sup> in the fiscal year ending April 30, 2004. </p>
  5. __EOS;
  6. $fnotenum = 0;
  7. if(preg_match($regexp, $target, $matches)){
  8.         $fnotenum = 0;
  9.         $replacements = "$1<span class='LOOKHERE' $2 $4>[".++$fnotenum."]$6";
  10.         $target = preg_replace($regexp, $replacements, $target);
  11. }
  12. echo $target;
  13. exit();
  14.  
The output I am expecting from this code and sample input should look like this:

<b>MANAGEMENT'S DISCUSSION AND ANALYSIS</b> <p>This text is shorter. <sup><span style="color: blue; font-size: 10px;" class="footnote">[1]</span></sup>.&nbsp; There has to be at least two sentences presented <sup><span style="color: blue; font-size: 10px;" class="footnote">[2]</span></sup>) discussion and analysis is designed to identify the significant <sup><span style="color: blue; font-size: 10px;" class="footnote">[3]</span></sup> in the fiscal year ending April 30, 2004. </p>

Each instance of the id attribute has been removed so that subsequent calls to preg_match will not find the nodes that have already been processed and the arbitrary footnote numbers assigned in the authoring system have been replaced by sequential values.

What I get is this:

<b>MANAGEMENT'S DISCUSSION AND ANALYSIS</b> <p>This text is shorter. <sup><span style="color: blue; font-size: 10px;" id="fn6" class="footnote">[6]</span></sup>.&nbsp; There has to be at least two sentences presented <sup><span style="color: blue; font-size: 10px;" id="fn4" class="footnote">[4]</span></sup>) discussion and analysis is designed to identify the significant <sup><span class='LOOKHERE' style="color: blue; font-size: 10px;" class="footnote">[1]</span></sup> in the fiscal year ending April 30, 2004. </p>

Notice that only the very last instance of the footnote span has been modified even though each individual span does match the regular expression. I don't understand this behavior. Am I misunderstanding the preg_replace function? Is my logic flawed? Anybody experienced and overcome this issue? Any suggestions?

Your time and thoughts are greatly appreciated,
Dan Ellison
Dec 8 '10 #1
1 3568
JKing
1,206 Expert 1GB
Hey there,

I'm not too great with regular expressions myself but I can shed a little light on things for you.

I added this line to your code.
Expand|Select|Wrap|Line Numbers
  1. echo preg_match_all($regexp, $target, $matches);
  2.  
preg_match_all returns only 1 match. You may need to rework your regular expression.
Dec 10 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Warren Oates | last post by:
If I have a textfile of words, each word on one line: dog cat viagra for instance, and I get them into an array, how then can I replace those words in a string with something else: "The...
1
by: sicapitan | last post by:
preg_match( "|<!-- InstanceBegin(.*)(-->)(.*)<!-- InstanceEnd -->|U", $contents, $matches ); $my_template = $matches; preg_match_all( "|<!-- InstanceBeginEditable(.*)(-->)(.*)<!--...
2
by: jason | last post by:
Hello. I've got this js function I call from an html input button. It finds the occurance of a string in a textbox, selects it and scrolls into view. JS newbie question, considering it's a...
8
by: Ric Da Force | last post by:
Hi, I have a string such as 'C1, C2, C3'. Without assuming that each bit of text is of fixed size, what is the easiest way to change this list so that it reads: 'C1, C2 and C3' regardless of...
2
by: Philo Hippo | last post by:
Hi, I need to be able to remove spaces from the beginning or the end of a string. This is my very 1st attempt at doing that in JS and here is what i got: function SearchForDomains(winName,...
2
by: Ed Brown | last post by:
I'm working on a VB.Net application that needs to do quite a bit of string pattern matching, and am having problems using the "LIKE" operator to match the same string twice in the pattern. For...
9
by: kd | last post by:
Hi All, I have a .txt file, in which I have to look for the last occurance of a keyword, and process contents of the file from that point onwards. How can this be achieved? Can anybody guide...
4
by: pnsreee | last post by:
Hi all, I have a string "Post.lang.tmp.txt" and i have to replase the ".txt" with "_large.txt". Please help me regarding this problem. I got it in shell scripting using `echo $string |...
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
2
by: rienh | last post by:
Hello all, I try to accomplish the following. I have a string like: “volkswagen-golf-gti” ** And I want it to change the string into: “Volkswagen Golf GTI” (last part completely uppercase) But...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.