繁体   English   中英

PHP从字符串处理HTML

[英]PHP Manipulating HTML from string

我正在从文本编辑器中读取HTML字符串,并且需要先处理一些元素,然后再将其保存到数据库中。

我所拥有的是这样的:

<h3>Some Text<img src="somelink.jpg" /></h3>

要么

<h3><img src="somelink.jpg" />Some Text</h3>

我需要将其放入以下格式

<h3>Some Text</h3><div class="img_wrapper"><img src="somelink.jpg" /></div>

这是我想出的解决方案。

$html = '<html><body>' . $field["data"][0] . '</body></html>';

$dom = new DOMDocument();
$dom->loadHTML($html);

$domNodeList = $dom->getElementsByTagName("img");

// Remove Img tags from H3 and place it before the H# tag
foreach ($domNodeList as $domNode) {
    if ($domNode->parentNode->nodeName == "h3") {
        $parentNode = $domNode->parentNode;
        $parentParentNode = $parentNode->parentNode;

        $parentParentNode->insertBefore($domNode, $parentNode->nextSibling);
    }
}

echo $dom->saveHtml();

您可能正在寻找一个preg_replace

// take a search pattern, wrap the image tag matching parts in a tag
// and put the start and ending parts before the wrapped image tag.
// note: this will not match tags that contain > characters within them,
//       and will only handle a single image tag
$output = preg_replace(
    '|(<h3>[^<]*)(<img [^>]+>)([^<]*</h3>)|',
    '$1$3<div class="img_wrapper">$2</div>',
    $input
);

我用答案更新了问题,但出于很好的考虑,这里还是在答案部分。

$html = '<html><body>' . $field["data"][0] . '</body></html>';

$dom = new DOMDocument();
$dom->loadHTML($html);

$domNodeList = $dom->getElementsByTagName("img");

// Remove Img tags from H3 and place it before the H# tag
foreach ($domNodeList as $domNode) {
    if ($domNode->parentNode->nodeName == "h3") {
        $parentNode = $domNode->parentNode;
        $parentParentNode = $parentNode->parentNode;

        $parentParentNode->insertBefore($domNode, $parentNode->nextSibling);
    }
}

echo $dom->saveHtml();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM