abdfahim Posted June 27, 2010 Share Posted June 27, 2010 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); Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 27, 2010 Share Posted June 27, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.