0

I'm looking for the best way to clean up old HTML tables (with PHP) so that they are correct HTML5 tables - it's mostly a matter of stripping not allowed attributes. In addition to that, I'd also like to strip inline styles of these tables. It would be really great if that can be accomplished in one go.

I have been researching regular expresions mostly, but after reading that regular expressions are not recommended to perform that, I am looking for something else that would help.

1
  • use DOMDocument, regex will be plagued by problems Commented Sep 15, 2016 at 8:06

1 Answer 1

1

A quick example of how you could use DOMDocument to strip attributes - could extend that to also add attributes but that is another matter.

$strhtml="
<table width='100%' cellpadding='10px' cellspacing='5px' border='2px'>
    <tr>
        <td align='left' valign='top'>banana</td>
    </tr>
</table>";

$remove=array('cellpadding','cellspacing','border','align','valign');


$dom=new DOMDocument;
$dom->loadHTML( $strhtml );

$elements=$dom->getElementsByTagName('*');
foreach( $elements as $node ){
    foreach( $remove as $attrib ){
        if( $node->hasAttribute( $attrib ) ){
            $node->removeAttribute( $attrib );
        }
    }
}

/* debug output */
echo '<textarea cols=100 rows=10>',$dom->saveHTML(),'</textarea>';
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that will do! I've figured it out before you posted the answer really, but that's exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.