在php中查找和Reg_replace标记html

问题描述 投票:0回答:1

我有以下代码:

$html = '<p>Lorem is sum<p>
         <h1><strong><em>@slide</strong></h1>
         <p>Lorem is sum</p>';
$html2 = 'Hello World';

我想在$ html“ @slide”中搜索,并将其更改为“ Hello World”。我已经尝试了以下代码

if (strpos($html, '@slide') !== false) {
    $result = str_replace('@slide',$html2,$html);
}

我有一个问题,我想删除H1-H6,<strong></strong><em></em>标签并将其更改为<p></p>标签。我想要以下结果:

$html = '<p>Lorem is sum<p>
         <p>Hello World</p>
         <p>Lorem is sum</p>';
php regex preg-replace
1个回答
0
投票

Regex通常不是处理HTML / XML的好方法(请参阅this Q&A)。这是一个DOMDocument解决方案:

DOMDocument

输出:

$html = '<p>Lorem is sum</p>
         <h1><strong><em>@slide</em></strong></h1>
         <p>Lorem is sum</p>';

$doc = new DOMDocument();
$doc->loadHTML("<div>$html</div>", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($doc);
$node = $xpath->query('//*[text()="@slide"]')[0];
do {
    // find next element sibling
    $n = $node->nextSibling;
    while (!($n instanceof DOMElement) && $n !== null) $n = $n->nextSibling;
    if ($n === null) continue;
    // if it's a <p>, back at top level
    if ($n->nodeName === 'p') break;
} while ($node = $node->parentNode);
// add the new <p> node
$node->parentNode->insertBefore($doc->createElement('p', 'Hello World'), $n);
// delete the old one
$node->parentNode->removeChild($node);
// output the result. Use substr to strip the <div> tag we added
echo substr($doc->saveHTML(), 5, -7);

<p>Lorem is sum</p> <p>Hello World</p><p>Lorem is sum</p>

© www.soinside.com 2019 - 2024. All rights reserved.