删除内联样式属性并使用php添加html属性

Sai*_*wal 0 css php attributes add

1)转换

<table style="width: 700px; height: 300px; background-color: #ff0000;" border="0">
Run Code Online (Sandbox Code Playgroud)

<table width="700" height="300" bgcolor="#ff0000" border="0">
Run Code Online (Sandbox Code Playgroud)

2)转换

<table style="width: 700px; background-color: #ff0000;" border="0">
Run Code Online (Sandbox Code Playgroud)

<table width="700" bgcolor="#ff0000" border="0">
Run Code Online (Sandbox Code Playgroud)

我正在使用Joomla 1.5.x,其中内容客户端可以添加表或嵌套表.Tinymce使用内联样式表格尺寸,背景属性.但是当我们尝试从前端生成pdf时,内联样式会被忽略.Joomla正在使用TCPDF来生成我已更新版本但同样的问题.当我将css属性转换为html属性时,它以格式化方式生成表.因此,想要将table,td,th的所有内联样式替换为html属性.尝试过来自这个网站的许多帖子,但由于我在正则表达式方面很差,因此无法使用它们.

请帮助我这样做.

提前致谢.

小智 6

我认为你可以使用PHP DOM.

1)使用DOMElement :: getAttribute()获取属性的值 style=

2)$split = explode(";", $style)用于分隔那些css值

3)每个条目$ i of $ split $attributes = explode(":", $split[$i]),in get属性的名称及其值.

4)现在你得到了包含2个值的$属性:该属性的属性和值.

5)使用DOMElement :: setAttribute()添加$ attributes的值.

所以,将所有内容都放入代码:

$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]);
}
Run Code Online (Sandbox Code Playgroud)

这种方式不涉及正则表达式:)但您需要修改它以适合您的上下文.