SoundreameR Posted April 28, 2007 Share Posted April 28, 2007 Hi guys.. I really hate RegEx, it's a mistery for me how to construct a regex pattern in order to make some changes in a string with preg_replace(). Sooo... here is my problem: // clean if tag //need to add somthing HERE $item = preg_replace('/<(\/?)(table|tr|td).*?>/is', '<$1$2>', $item); print_r($item); $item carries a html string, actually a html table which will be filtered and parsed into an array. This is a part of an open source class which provides parsing a html table in to an array. But it cleans any code contained in <td> such as <td bgcolor=#FFCC99>. All I need is, before the tags are being cleaned, the bgcolor hex value to be moved inside the table cell. Example: If there is <td bgcolor=#FFCC99> in the string somewhere or <td bgcolor=#FFFFFF> no mather of the hex code, it will be replaced with <td>#FFCC99| or like the other example <td>#FFFFFF| so I can later parse the cell content to bgcolor and actual value with something like this explode("|", $string); I hope my bad english was not fatal on this explanation Link to comment https://forums.phpfreaks.com/topic/49056-preg_replace-hearthache/ Share on other sites More sharing options...
c4onastick Posted April 29, 2007 Share Posted April 29, 2007 Here's one solution, this gets a little touchy because you want 'td' to be there but not be part of the match. $item = preg_replace('%(?<=td) bgcolor=(#[A-Z]{6})>%', ">$1|", $item); It's pretty specific to your application, it'll only pick up 'bgcolor' in a 'td' tag. Give that a shot, if it's too specific we can add a few things to make it a little bit more flexible. Link to comment https://forums.phpfreaks.com/topic/49056-preg_replace-hearthache/#findComment-240811 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.