473,387 Members | 1,891 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.

Regex replace doesn't catch nesting properly.

1
I am building a CMS driven website in php and I have a list of terms that for each page content I need to search for and replace with the following code:

<a href="[link]">[term]</a>

The problem I have is that a term can appear in another term, for example garden furniture, hire garden furniture. When I am doing a basic preg_replace it means that the term hire garden furniture has a nested link tag because it is found for both terms. What I need to do is ignore any term that ends with </a> so that they only get replaced once. The function I am using is below and I have commented the parts that don't seem to be working correctly.

Expand|Select|Wrap|Line Numbers
  1. $html_body = "We have a wide variety of garden furniture for any occasion. You can hire garden furniture from us for use at parties, corporate functions etc.";
  2.  
  3. private function _replaceText($html_body)
  4. {
  5. $terms = array("hire garden furniture", "garden furniture", "office furniture");
  6.  
  7. foreach($terms as $t)
  8. {
  9. $replace_string = '$1';
  10.  
  11. $pattern attempt1 = '/('.$row['term'].')/i'; //This replaces all the terms with $replace_string above but hire garden furniture results in a nested link
  12. $pattern attempt2 = '/('.$row['term'].'[^()])/i'; //This does not replace anything and the content is returned as it was
  13. $pattern_attempt3 = '/(?(?=('.$row['term'].'<........a>))(notmatch)|(('.$row['term'].')))/i'; //This replaces the terms but does not recognise $1 in the replace_string and still results in a nested link
  14.  
  15. $html_body = preg_replace($pattern, $replace_string, $html_body);
  16. }
  17.  
  18. return $html_body;
  19. }//end function
  20.  
The results I am looking for when the function returns $html_body is:

We have a wide variety of <a href="[link]">garden furniture</a> for any occasion. You can <a href="[link]">hire garden furniture</A> from us for use at parties, corporate functions etc.

In a nutshell I am looking for a pattern that only replaces a given term if its is not already within a link tag, for example:

Hire garden furniture - this would be replaced
<a href="/garden_furniture">hire garden furniture</a> - this would be ignored

Any help with this would be greatly appreciated.

Many thanks.
Jun 8 '07 #1
1 1554
pbmods
5,821 Expert 4TB
Changed thread title to better describe the problem (and to remove annoying 'please help').

Heya, mlaw40. Welcome to TSDN!

Try explodeing your string by '</a>'.
Jun 8 '07 #2

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

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
1
by: Ada | last post by:
i'm trying to use Regex to match a 4 number group pattern. once a match is found, write it to RichTextBox in red. test data: This is my 4567 test data. 1234this is another line of data. Here's...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
6
by: Chris | last post by:
I'm new at using Regex, and I'm using VB.NET. I can't seem to get this to work. I'm replacing matched items in a text file. The format is v (upper or lower) and then 4-5 integers. I'm trying (many...
6
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other...
6
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to replace double quotes inside the text from the database with " to correctly display my text. I was trying to use Regex to perform such a task: Regex.Replace(text, "", """) to...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.