使用php删除内联样式属性并添加html属性
1) 皈依使用php删除内联样式属性并添加html属性,php,css,attributes,add,Php,Css,Attributes,Add,1) 皈依 到 2) 皈依 到 我使用的是Joomla 1.5.x,其中content client可以添加表或嵌套表。Tinymce对表格维度、背景属性使用内联样式。但当我们尝试从前端生成pdf时,表的内联样式被忽略。Joomla正在使用TCPDF生成我已经更新了版本,但存在相同的问题。当我将css属性转换为html属性时,它以格式化的方式生成表。因此,我们认为应该替换table、td、th到html属性的所有内联样式。尝试了这个网站的许多线程,但无法使用它们,因为我不擅长正则
到
2) 皈依
到
我使用的是Joomla 1.5.x,其中content client可以添加表或嵌套表。Tinymce对表格维度、背景属性使用内联样式。但当我们尝试从前端生成pdf时,表的内联样式被忽略。Joomla正在使用TCPDF生成我已经更新了版本,但存在相同的问题。当我将css属性转换为html属性时,它以格式化的方式生成表。因此,我们认为应该替换table、td、th到html属性的所有内联样式。尝试了这个网站的许多线程,但无法使用它们,因为我不擅长正则表达式
请帮我做这件事
提前感谢。我想您可以使用PHP DOM来实现这一点 1) 使用doElement::getAttribute()获取属性
style=
2) 使用$split=explode(;”,$style)
分隔这些css值
3) 对于$split的每个条目$i,$attributes=explode(“:”,$split[$i])
,在get属性的名称及其值中
4) 现在,$attributes包含两个值:attribute和该属性的值
5) 使用doElement::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]);
}
$dom=newdomdocument();
$dom->loadHTML($html);
$atrvalue=$dom->getAttribute(“样式”);
$split=explode(“;”,$atrvalue);
对于($i=0;$icreateElement(“表”);
$newnode=$doc->appendChild($node);
$newnode->setAttribute($attribute[0],$attribute[1]);
}
这种方法不涉及regex:),但您需要修改它以适应您的上下文。我认为您可以使用PHP DOM来实现这一点 1) 使用doElement::getAttribute()获取属性
style=
2) 使用$split=explode(;”,$style)
分隔这些css值
3) 对于$split的每个条目$i,$attributes=explode(“:”,$split[$i])
,在get属性的名称及其值中
4) 现在,$attributes包含两个值:attribute和该属性的值
5) 使用doElement::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]);
}
$dom=newdomdocument();
$dom->loadHTML($html);
$atrvalue=$dom->getAttribute(“样式”);
$split=explode(“;”,$atrvalue);
对于($i=0;$icreateElement(“表”);
$newnode=$doc->appendChild($node);
$newnode->setAttribute($attribute[0],$attribute[1]);
}
这种方法不涉及regex:),但您需要修改它以适应您的上下文。Le:谢谢。但是我的$html不仅仅有表标记,它是带有p、div、span、b、表标记的html内容。只有当我只有一张桌子的时候,你的回答才会有帮助。你的问题解决了吗?我认为你有什么标记并不重要,你可以把可能的标记放在数组中,比如$arr=array('table','a','p','span'),然后使用循环进行处理:)我不能给你确切的答案,当你能自己找到它的时候:)乐:谢谢。但是我的$html不仅仅有表标记,它是带有p、div、span、b、表标记的html内容。只有当我只有一张桌子的时候,你的回答才会有帮助。你的问题解决了吗?我认为不管你有什么标记,你可以把可能的标记放在数组中,比如$arr=array('table','a','p','span'),然后使用循环进行处理:)当你自己能找到它时,我不能给你确切的答案:)
<table style="width: 700px; background-color: #ff0000;" border="0">
<table width="700" bgcolor="#ff0000" border="0">
$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]);
}