Reputation: 13315
$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';
$text_2 = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $text);
OUTPUT(i have given the html format here):
<p>
<strong>hello</strong>
</p>
<table></table>
My problem is all attributes must be removed but not the attributes belongs to table. That is i am expecting the out put exactly like below(HTML FORMAT):
<p>
<strong>hello</strong>
</p>
<table style="text-align:center"></table>
What should i need to modify in the above regular expression to achieve it..
Any help will be thankful and grateful....
Thanks in advance...
Upvotes: 0
Views: 131
Reputation: 54659
If you want to avoid using regex, because you really souldn't use regex to work on xml/html structures, try:
<?php
$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';
$dom = new DOMDocument;
$dom->formatOutput = true;
$dom->loadHtml($text);
$xpath = new DOMXpath($dom);
foreach ($xpath->query('//*[not(name()="table")]/@*') as $attrNode) {
$attrNode->ownerElement->removeAttributeNode($attrNode);
}
$output = array();
foreach ($xpath->query('//body/*') as $childNode) {
$output[] = $dom->saveXml($childNode, LIBXML_NOEMPTYTAG);
}
echo implode("\n", $output);
Output:
<p>
<strong>hello</strong>
</p>
<table style="text-align:center"></table>
Upvotes: 3
Reputation: 14891
You are very close with your current reg-ex. You need to do a check (think it is a negative look-ahead in this case?)
<(?!table)([a-z][a-z0-9]*)[^>]*?(\/?)>
What that first bit of reg-ex is doing is checking that it does not start with 'table', then it is your regex.
Upvotes: 1
Reputation: 43265
Bit of hacky solution, but works . Try disabling TABLE tags for a while in your code, and enable them again. It would work.
see : http://codepad.org/nevLWMq8
<?php
$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';
/* temporary change table tags with something not occuring in your HTML */
$textTemp = str_replace(array("<table","/table>"),array('###','+++'),$text);
$text_2 = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $textTemp);
echo "\n\n";
/* restore back the table tags */
$finalText = str_replace(array("###","+++"),array("<table","/table>"),$text_2);
echo $finalText ;
?>
Upvotes: 0