Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用PHP根据CSS设置更改标记类型_Html_Css_Dom_Dom Traversal - Fatal编程技术网

Html 使用PHP根据CSS设置更改标记类型

Html 使用PHP根据CSS设置更改标记类型,html,css,dom,dom-traversal,Html,Css,Dom,Dom Traversal,我需要从HTML文件中读取数据。HTML文件包含一个带有CSS规则的元素。根据CSS规则,我需要将更改为,, 不幸的是,我不能基于类名或ID,因为它们会改变 这在DOM遍历中是可能的吗 我想要这个: <!DOCTYPE html> <html> <head> <style> .exampple1{font-weight:700} .exampple2{font-style:italic} .exampple3{text-decoration:und

我需要从HTML文件中读取数据。HTML文件包含一个带有CSS规则的元素。根据CSS规则,我需要将
更改为

不幸的是,我不能基于类名或ID,因为它们会改变

这在DOM遍历中是可能的吗

我想要这个:

<!DOCTYPE html>
<html>
<head>
<style>
.exampple1{font-weight:700}
.exampple2{font-style:italic}
.exampple3{text-decoration:underline}
.exampple4{font-weight:700;font-style:italic;text-decoration:underline}
.exampple5{font-weight:700;font-style:italic}
.exampple6{font-weight:700;text-decoration:underline}
.exampple7{font-style:italic;text-decoration:underline}
</style>
</head>

<body>
<p><span class="exampple1">bold text</span></p>
<p><span class="exampple2">italic text</span></p>
<p><span class="exampple3">underline text</span></p>
<p><span class="exampple4">bold, italic and underline</span></p>
<p><span class="exampple5">bold and italic</span></p>
<p><span class="exampple6">bold and underline</span></p>
<p><span class="exampple7">italic and underline</span></p>
</body>
</html>
<!DOCTYPE html>
<html>

<body>
<p><span class="exampple1"><b>bold text</b></span></p>
<p><span class="exampple2"><i>italic text</i></span></p>
<p><span class="exampple3"><u>underline text</u></span></p>
<p><span class="exampple4"><b><i><u>bold, italic and underline</u></i></b></span></p>
<p><span class="exampple5"><b><i>bold and italic</i></b></span></p>
<p><span class="exampple6"><b><u>bold and underline</u></b></span></p>
<p><span class="exampple7"><i><u>italic and underline</u></i></span></p>
</body>
</html>

.example1{font-weight:700}
.example2{font-style:italic}
.example3{文本装饰:下划线}
.example4{字体大小:700;字体样式:斜体;文本装饰:下划线}
.example5{字体大小:700;字体样式:斜体}
.example6{font-weight:700;文本装饰:下划线}
.example7{字体样式:斜体;文本装饰:下划线}
粗体文本

斜体文本

下划线文本

粗体、斜体和下划线

粗体斜体

粗体加下划线

斜体加下划线

变成这样:

<!DOCTYPE html>
<html>
<head>
<style>
.exampple1{font-weight:700}
.exampple2{font-style:italic}
.exampple3{text-decoration:underline}
.exampple4{font-weight:700;font-style:italic;text-decoration:underline}
.exampple5{font-weight:700;font-style:italic}
.exampple6{font-weight:700;text-decoration:underline}
.exampple7{font-style:italic;text-decoration:underline}
</style>
</head>

<body>
<p><span class="exampple1">bold text</span></p>
<p><span class="exampple2">italic text</span></p>
<p><span class="exampple3">underline text</span></p>
<p><span class="exampple4">bold, italic and underline</span></p>
<p><span class="exampple5">bold and italic</span></p>
<p><span class="exampple6">bold and underline</span></p>
<p><span class="exampple7">italic and underline</span></p>
</body>
</html>
<!DOCTYPE html>
<html>

<body>
<p><span class="exampple1"><b>bold text</b></span></p>
<p><span class="exampple2"><i>italic text</i></span></p>
<p><span class="exampple3"><u>underline text</u></span></p>
<p><span class="exampple4"><b><i><u>bold, italic and underline</u></i></b></span></p>
<p><span class="exampple5"><b><i>bold and italic</i></b></span></p>
<p><span class="exampple6"><b><u>bold and underline</u></b></span></p>
<p><span class="exampple7"><i><u>italic and underline</u></i></span></p>
</body>
</html>

粗体文本

斜体文本

下划线文本

粗体、斜体和下划线

粗体斜体

粗体加下划线

斜体加下划线


DOM遍历是在JS中完成的,所以我认为这样的解决方案对您有用吗


在JS中,您可以解析
元素的内容。然后,您可以使用每个已解析CSS规则中的选择器来查询元素,并使用
element.style.fontwweight='bold'在这些元素上设置CSS属性例如。

那么您正在寻找一些服务器端PHP代码来计算有效的样式,然后将这些信息转换成普通的HTML标记?段落或跨度上的样式属性是否不起作用?不,很遗憾,样式不起作用,因为每个类的样式设置可以包含多个设置。另外,我不想在HTML中使用任何内联样式。但如果这仅适用于这些样式设置,我可以进行stringreplace。请让我知道,如果有可能添加的风格atributes。感谢您的建议,但我只能使用PHP(很抱歉我错误地标记了DOM遍历,我认为它在PHP中的调用也是如此)。