首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除图像的包装标签

删除图像的包装标签
EN

Stack Overflow用户
提问于 2017-05-04 01:34:33
回答 4查看 299关注 0票数 2

我有一个CKeditor,在图像周围输出一些标签。到目前为止,我正在使用正则表达式来消除这些包装标签。

下面是一些测试字符串:

代码语言:javascript
复制
$example1 = '<p data-entity-type="" data-entity-uuid="" style="text-align: center;"><span><img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620" /><span title="Click and drag to resize">•</span></span></p>';
$example2 = '<p><img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620" /></p>';
$example3 = '<html>
<head></head>
<body>
some text here...
<p><img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620" />
</p>
</body>
</html>';
// Wanted result : <html><head></head><body>some text here...<img alt="julie-bishop.jpg" data-entity-type="" data-entity-uuid="" height="349" src="/sites/default/files/inline-images/julie-bishop.jpg" width="620" /></body></html>

我尝试过的正则表达式是/(.*?)<p>\s*(<img[^<]+?)\s*<\/p>(.*)/,它与example2完美地工作在一起。

代码语言:javascript
复制
preg_replace("/(.*?)<p>\s*(<img[^<]+?)\s*<\/p>(.*)/", "$1$2$3", $string);

规则是:如果您检测到一个with作为其子类之一,那么保持和移除它和它的其他子级(可以是span或其他.)

知道怎么实现我所需要的吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-05-08 10:10:17

您可以使用以下正则表达式:

代码语言:javascript
复制
<p(?:[^>]*|\r\n|\n)>(?:.*|\r\n|\n)(<img(?:[^>]*|\r\n|\n)>)(?:.*|\r\n|\n)<\/p>

下面是regex101.com上的演示

下面是工作演示 in eval.in (您的PHP代码)

票数 2
EN

Stack Overflow用户

发布于 2017-05-08 13:18:57

它不是使用regexp,但如果使用任何xml解析器(如DOM ),则可以使用更易读的方法。

有些人在遇到问题时会想:“我知道,我会使用正则表达式。”现在他们有两个问题。-杰米·扎温斯基( Jamie Zawinski)的一句名言:

您可以使用http://php.net/manual/en/domdocument.loadhtml.php加载html片段。然后可以使用http://php.net/manual/en/domdocument.getelementsbytagname.php获取所有的<p>。一旦您获得了<p>标记的节点列表,您就可以循环遍历每个节点。

在每个<p>节点上使用use,然后可以使用http://php.net/manual/en/domdocument.getelementsbytagname.php查找任何<img>标记。如果有发现,可以使用$node->childNodes获取每个<p>节点的子节点。循环并使用http://php.net/manual/en/domnode.removechild.php删除<img>节点以外的子节点。完成后,您可以使用http://php.net/manual/en/domdocument.savehtml.php将处理后的html返回。

票数 0
EN

Stack Overflow用户

发布于 2017-05-08 18:26:04

您正在应用的方法不太好,而不是REGEX,您应该使用DOMDocument。这里我们使用的是DOMDocumentDOMXPath。我希望我的解决方案能帮你解决问题,并且肯定能解决你的问题。

代码语言:javascript
复制
<?php
ini_set('display_errors', 1);
$example1 = '<p data-entity-type="" data-entity-uuid="" style="text-align: center;"><span><img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620" /><span title="Click and drag to resize">•</span></span></p>';
$example2 = '<p><img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620" /></p>';
$example3 = '<html>
<head></head>
<body>
some text here...
<p><img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620" />
</p>
</body>
</html>';


$domDocument= new DOMDocument();
$domDocument->loadHTML($example1,LIBXML_HTML_NOIMPLIED);
$domXPath=new DOMXPath($domDocument);

if($domXPath->query("//html")->length)
{
    foreach($domXPath->query("//p") as $pelement)
    {
        if($domXPath->query("//img",$pelement)->length)
        {
            $pelement->parentNode->replaceChild(getReplacement($domXPath),$pelement);
        }
    }
    echo $pelement->ownerDocument->saveHTML();
}
else
{
    echo getReplacement($domXPath,true);
}

function getReplacement($domXPath,$string=false)
{
    global $domDocument;
    $results=$domXPath->query('//p');
    foreach($results as $result)
    {
        if($innerNodes=$domXPath->query("//img",$result->childNodes->item(0)))
        {
            if($string===true)
            {
                return $domDocument->saveHTML($result->childNodes->item(0));
            }
            else 
            {
                return $result->childNodes->item(0);
            }
        }
    }
}

输出string1:

<span><img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620"><span title="Click and drag to resize">•</span></span>

输出string2:

<img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620">

输出string3:

<html> <head></head> <body> some text here... <img alt="image.jpg" data-entity-type="" data-entity-uuid="" height="349" src="image.jpg" width="620"> </body> </html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43772453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档