Jump to content

preg_match question


abdfahim

Recommended Posts

Hi,

 

It seems that I have caught in a very simple looking riddle. I have been trying to convert a table so that each <TR> is replaced by /n and each <TD> by /t. Please tell me what wrong I have done?

 

$a = "<table border=0><tr bgcolor='#D6D6D6'><td align='center'>Volume 1</td><td align='center'>Volume 2</td></tr></table>";
            $a = preg_replace("/([<]table.*[>])(.*)([<]\/table[>])/i","$2",$a);
            $a = preg_replace("/([<]tr.*[>])(.*)([<]\/tr[>])/i","/n$2",$a);
            $a = preg_replace("/([<]td.*[>])(.*)([<]\/td[>])/i","/t$2",$a);

 

 

Link to comment
https://forums.phpfreaks.com/topic/205993-preg_match-question/
Share on other sites

Corrected the regular expressions, also I'm pretty sure you want to use \t and \n

 

$a = "<table border=0><tr bgcolor='#D6D6D6'><td align='center'>Volume 1</td><td align='center'>Volume 2</td></tr></table>";
$a = preg_replace("/<table[^>]*>(.*?)<\/table>/i", "$1", $a);
$a = preg_replace("/<tr[^>]*>(.*?)<\/tr>/i", "\n$1", $a);
$a = preg_replace("/<td[^>]*>(.*?)<\/td>/i", "\n\t$1", $a);

echo $a;

//Output:
//
//	Volume 1
//	Volume 2

 

NOTE: moved topic to RegEx forum.

Link to comment
https://forums.phpfreaks.com/topic/205993-preg_match-question/#findComment-1077900
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.