Jump to content

preg_replace hearthache


SoundreameR

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.