remove inline style properties and add html attributes using php
1) convert
<table style="width: 700px; height: 300px; background-color: #ff0000;" border="0">
to
<table width="700" height="300" bgcolor="#ff0000" border="0">
2) convert
<table style="width: 700px; background-color: #ff0000;" border="0">
to
<table width="700" bgcolor="#ff0000" border="0">
I'm using Joomla 1.5.x where in content client can add table or nested tables. Tinymce using inline styles for table dimensions, background properties. But when we try to generate pdf from front end inline styles for table ar开发者_开发技巧e ignored. Joomla is using TCPDF to generate I've updated the version but same problem. When I've converted the css properties into html attributes, it is generating the table in formated way. So thought to replace all inline styles for table, td, th to html attributes. Tried with many thread from this website but unable to make use of them as I'm poor at regular expressions.
Please any help me in doing so.
Thanks in advance.
I think you can use PHP DOM for this.
1) Use DOMElement::getAttribute() to get the value of attribute style=
2) Use $split = explode(";", $style)
to separate those css values
3) With each entry $i of $split, $attributes = explode(":", $split[$i])
, in get attribute's name and its value.
4) Now you got $attributes holding 2 values: attribute and value of that attribute.
5) Use DOMElement::setAttribute() to add those values of $attributes.
So, put everything into codes:
$dom = new DOMDocument();
$dom->loadHTML($html);
$atrvalue= $dom->getAttribute("style");
$split = explode(";", $atrvalue);
for ($i=0; $i<=count($split); $i++) {
$attribute = explode(":", $split[$i]);
$node = $doc->createElement("table");
$newnode = $doc->appendChild($node);
$newnode->setAttribute($attribute[0], $attribute[1]);
}
This way doesn't involve regex :) But you need to modify it in order to fit your context.
精彩评论