我有一个CKeditor,在图像周围输出一些标签。到目前为止,我正在使用正则表达式来消除这些包装标签。
下面是一些测试字符串:
$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完美地工作在一起。
preg_replace("/(.*?)<p>\s*(<img[^<]+?)\s*<\/p>(.*)/", "$1$2$3", $string);规则是:如果您检测到一个with作为其子类之一,那么保持和移除它和它的其他子级(可以是span或其他.)
知道怎么实现我所需要的吗?
发布于 2017-05-08 10:10:17
发布于 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返回。
发布于 2017-05-08 18:26:04
您正在应用的方法不太好,而不是REGEX,您应该使用DOMDocument。这里我们使用的是DOMDocument和DOMXPath。我希望我的解决方案能帮你解决问题,并且肯定能解决你的问题。
<?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>
https://stackoverflow.com/questions/43772453
复制相似问题