只需交换元素就可以通过这个线程实现,
Replace Tag in HTML with DOMDocument
. 这里有一个扩展方法,它只影响具有该样式属性的元素。
$html = '<p><span style="color:#2c2c2c;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">Strong content</span></p><ul><li><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">list item</span></li><li><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">list item</span></li><li><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">list item</span></li><li><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">list item</span></li><li><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">list item</span></li><li><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">list item</span></li></ul><p><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">Content text</span></p><p><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">Content text</span></p><p><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">Content text</span></p><p><span style="font-size:10.5pt;color:#2c2c2c;font-weight:700">Should be bold</span><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">: regular text</span></p><p><span style="font-size:10.5pt;color:#2c2c2c;font-weight:700">Should be bold</span><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">: regular text </span></p><p><span style="font-size:10.5pt;color:#2c2c2c;font-weight:700">Should be bold</span><span style="color:#2c2c2c;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:10.5pt;font-family:"Arial";font-style:normal">: regular text</span></p>';
$dom = new domdocument();
$dom->loadhtml($html);
$elements = $dom->getElementsByTagName("span");
for ($i = $elements->length - 1; $i >= 0; $i --) {
if(preg_match('/font-weight:700/', $elements[$i]->getattribute('style'))) {
$nodePre = $elements->item($i);
$nodeDiv = $dom->createElement("strong", $nodePre->nodeValue);
$nodePre->parentNode->replaceChild($nodeDiv, $nodePre);
}
}
echo $dom->savehtml();
https://3v4l.org/Y7Rua
替代方案:
if(preg_match('/font-weight:700/', $elements[$i]->getattribute('style'))) {
strpos
也可以使用,我猜你可能有空格,所以我使用了regex版本。
if(strpos($elements[$i]->getattribute('style'), 'font-weight:700') !== FALSE) {
https://3v4l.org/uqWpj
为了回答为什么你的regex切割比你想要的多,这是因为
<span.*
比赛
<span style="color:#2c2c2c;font-weight:400;
一直往前走直到它找到
font-weight:700
. 然后它捕获元素之后的内容,所有中间数据都将丢失。这就是为什么不应该使用regex进行解析的原因,它不知道元素。