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

Using regex to add multiple restrict conditions to preg_replace array

1
I have a great little script that will search a file and replace a list of words with their matching replacement word. I have also found a way to prevent preg_replace from replacing those words if they appear in anchor tags, img tags, or really any one tag I specify. I would like to create an OR statement to be able to specify multiple tags. To be clear, I would like to prevent preg_replace from replacing words that not only appear in an anchor tag, but any that appear in an anchor,link,embed,object,img, or span tag. I tried using the '|' OR operator at various places in the code with no success.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $data = 'somefile.html';
  3. $data = file_get_contents($data);
  4. $search = array ("/(?!(?:[^<]+>|[^>]+<\/a>))\b(red)\b/is","/(?!(?:[^<]+>|[^>]+<\/a>))\b(white)\b/is","/(?!(?:[^<]+>|[^>]+<\/a>))\b(blue)\b/is");
  5. $replace = array ('Apple','Potato','Boysenberry');
  6. echo preg_replace($search, $replace, $data);?>
  7. print $data;
  8. ?>
looking at the first search term which basically says to search for "red" but not inside <a> </a>:
Expand|Select|Wrap|Line Numbers
  1. "/(?!(?:[^<]+>|[^>]+<\/a>))\b(red)\b/is"
I am trying to figure out how I can keep preg_replace out of not just the </a> tag, but also the other tags listed.
Jan 11 '11 #1
0 1334

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

Similar topics

16
by: Stephane | last post by:
Hi, I'm trying to replace parenthesis using Regex.replace but I'm always having this error: System.ArgumentException: parsing ":-)" - Too many )'s. Parameter name: :-) Here's my code: ...
4
by: Ya Ya | last post by:
Hi, I have a string with some fixed text and variable text. For example: "this is a fixed text THE NEEDED INFO more more fixed text". How do I get the the variable text (THE NEEDED INFO) from this...
2
by: lgbjr | last post by:
Hi All, Is there a way to sort multiple dimensions of an array in VB.NET? Let's say I have the following in a multi-dimension array: 3081 100 2 3081 100 1 3081 20 1 3081 1 2 3021 100 2
6
by: pinetaj | last post by:
Hello, I have a question of using 'property' on accessing elements of array. There is an array member in a class. I'd like to restrict accessing the elements of the array through property. And...
4
by: Joe | last post by:
I need to do a find/replace on a column name in DataColumn.Expression. Is there a way to do the following using RegEx? MyColumn 10 and Desc = "This is MyColumn desc" I need to replace the...
6
by: Nightcrawler | last post by:
Hi all. I have a html table with multiple rows (one row example below). I would like to extract everything within the <tdtags into groups on a row by row basis. The process would be: find the...
2
by: KK | last post by:
Dear All I have a string like this: myOutput = myObject.MyMethod(myInput1,myInput2) I would like to parse this string and separate it into 4 groups. group1 contains left of '='. group2 contains...
0
by: rmeshksar | last post by:
Hi, I would like to do text replacement using RegEx and use the following statement: Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase) It works fine in all cases except in the...
2
by: daveftl | last post by:
Hello, i've tried to extract certain data using Regex in a File. but it seems not working.No errors and warnings have been found. here is my code: Private Sub extractTxt(ByVal inputFile As...
6
Ciary
by: Ciary | last post by:
hi, i have a problem i can't figure out. i'm trying to create a tree structure in a class. to do that i have a class "Knod" and a class "Leaf". the class "Tree" is an ArrayList of arrays. and...
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: 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
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
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
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,...

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.