我无法正确使用此正则表达式,也看不到缺少的内容。 See Regex101 example或以下细目:
正则表达式
<span.*?font-weight:700.*?>(.*?)<\/span>
我正在尝试查找每个包含font-weight:700的span实例。
<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>
获取该范围的内容并将其替换为
<strong>$1</strong>
问题在于这是我的结果:
<p><strong>Strong content</strong></p><ul><li><strong>Should be bold</strong><strong>Should be bold</strong><strong>Should be bold</strong><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>
剪切掉所有列表项,并在第2和第3场比赛后删除“常规文本”。
预期输出为:
<p><strong>Strong content</strong></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><strong>Should be bold</strong><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><strong>Should be bold</strong><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><strong>Should be bold</strong><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>
答案 0 :(得分:0)
仅交换元素可以通过此线程Replace Tag in HTML with DOMDocument实现。这是一种扩展方法,仅影响具有该style属性的元素。
$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();
替代:
if(preg_match('/font-weight:700/', $elements[$i]->getattribute('style'))) {
strpos
也可以使用,我猜您可能会有空格,所以我使用了正则表达式版本。
if(strpos($elements[$i]->getattribute('style'), 'font-weight:700') !== FALSE) {
正则表达式之所以能得到比您想要的削减更多的答案,是因为<span.*
与<span style="color:#2c2c2c;font-weight:400;
相匹配并且一直持续到找到font-weight:700
为止。然后,它将捕获该元素之后的内容,并且所有中间数据都将丢失。这就是为什么不应该使用正则表达式进行分析的原因,因为它不知道元素。
答案 1 :(得分:0)
您的正则表达式不起作用的原因是某些 span 标签不包含该字体粗细。
这将导致正则表达式部分name = 'A'
nick = 'B'
rep = 3
namenick = "#{name}#{nick}"
"#{ namenick }#{ " #{namenick}"*(rep-1) }"
#=> "AB AB AB"
继续匹配,直到找到具有
的span标签。
字体粗细。
此正则表达式将匹配项限制为包含该font-weight的有效标签。
查找:
.*?
替换:/<span(?=\s)(?=(?:[^>"']|"[^"]*"|'[^']*')*?\sstyle\s*=\s*(?:(['"])(?:(?!\1)[\S\s])*?font-weight:700(?:(?!\1)[\S\s])*\1))\s+(?:"[\S\s]*?"|'[\S\s]*?'|[^>]*?)+>([\S\s]*?)<\/span\s*>/
更多正则表达式信息:
<strong>$2</strong>