Issue
I am trying to clean up some HTML values that are imported from other sources, primarily email message bodies. Ideally, I would like to be able to strip <style>
and <link>
tags, but also apply their styles (assuming the link tag points to a style sheet) to the appropriate elements within the value.
I am currently trying to accomplish this via tidy. If it comes down to it, I can strip out the tags without applying the styles.
Solution
you could use php's simple-xml functions to assign the styles to your markup.
or write some custom implementation to manipulate the html (which is not a good practice tbh) if you don't want to get into the simplexml lib.
$dirtyInput = '<!-- your html string -->';
$regex = '/<style[^>]>(.*?)</style>/is';
if (preg_match_all($regex, $dirtyInput, $inlineStyles)) {
// your inline styles will be in $inlineStyles
$cleanedOutput = preg_replace($regex . 'g', '', $dirtyInput );
}
$regex = '/<link[^>]*href="([^"]+)"*[^>]*>/is';
if (preg_match_all($regex, $dirtyInput, $externalFiles)) {
// hrefs of external stylesheets $externalFiles;
$cleanedOutput = preg_replace($regex . 'g', '', $cleanedOutput);
}
// more code to read styles from external css files and apply their styles and the inline styles to your markup (not a quick task...)
Answered By - Andreas Linden
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.