Php 取消图像链接,删除未关闭的p并删除所有样式

Php 取消图像链接,删除未关闭的p并删除所有样式,php,html,html-parsing,domdocument,Php,Html,Html Parsing,Domdocument,我的Wordpress帖子有一些问题,我正在尝试使用DOMDocument来修复它们 第一个问题是,我的图像位于标记内,我想删除标记 我还想删除所有未关闭的标记,这些标记没有,我想从所有元素中删除样式 我可以发布一些我尝试过的代码,但我认为这一点都没有帮助,因为我没有用它。目前我只尝试从图片中删除链接,但似乎没有任何效果。我真的不太明白如何很好地使用DOMDocument子元素 这里您可以看到一个需要修复的HTML示例: 稍后编辑: 这是我尝试的,它似乎取消了图像链接,但只有图像编号1,3,5,

我的Wordpress帖子有一些问题,我正在尝试使用DOMDocument来修复它们

第一个问题是,我的图像位于标记内,我想删除标记

我还想删除所有未关闭的标记,这些标记没有

,我想从所有元素中删除样式

我可以发布一些我尝试过的代码,但我认为这一点都没有帮助,因为我没有用它。目前我只尝试从图片中删除链接,但似乎没有任何效果。我真的不太明白如何很好地使用DOMDocument子元素

这里您可以看到一个需要修复的HTML示例:

稍后编辑:

这是我尝试的,它似乎取消了图像链接,但只有图像编号1,3,5,7,而2,4,6保持不变

<img width="750" height="500" src="http://fancycribs.com/wp-content/uploads/2013/05/Modern-Riverside-Apartment-–-A-Stylish-and-Elegant-Residence-6.jpg" class="attachment-large wp-post-image" alt="Modern Riverside Apartment – A Stylish and Elegant Residence (6)" />        <p>This modern seventh floor riverside apartment is placed in the luxurious and modern Montevetro Building, which is close to Battersea Square with access to Chelsea, Fulham and Kings Road by crossing Battersea Bridge, London. This residence has become one of the iconic buildings in the Battersea area.</p>
<p>It offers spectacular views over the serene tranquility of the river. This apartment offers comfort and luxury throughout its double reception room, three bedrooms, three bathrooms and large decked balcony. The design details are astonishing: mahogany wood floors, original hand painted walls, large floor to ceiling windows offering a spectacular view over the river. The apartment is spacious, the space between living room and dining room is fluid, having continuity. The hall is large and has a lot of storage spaces, having the quality to link rooms one to another. The kitchen space is large and has plenty of storage capacity. It is dressed up in mahogany wood, offering personality and contrast and access to the large balcony.</p>
<p>The master bedroom is a masterpiece of style and elegance, with nice and simple furniture, a bathroom and accompanied by two further double bedrooms, a family bathroom and a shower room. The residence overwhelms you through its luxury and the splendid view.</p>
<p style="text-align: center"><a href="http://fancycribs.com/37216-modern-riverside-apartment-a-stylish-and-elegant-residence.html/modern-riverside-apartment-a-stylish-and-elegant-residence-7" rel="attachment wp-att-39033" class="local-link"><img class="aligncenter size-medium wp-image-39033" alt="Modern Riverside Apartment – A Stylish and Elegant Residence" src="http://fancycribs.com/wp-content/uploads/2013/05/Modern-Riverside-Apartment-–-A-Stylish-and-Elegant-Residence-7-670x446.jpg" width="670" height="446" title="Modern Riverside Apartment – A Stylish and Elegant Residence" /></a></p>
<p style="text-align: center">

谢谢

能否尝试运行此代码,看看您是否满意。这发现我已经设法用DOMDocument和HTML净化器做到了这一点

代码如下:

$p = "/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*<img.*)<\/a>/siU";
$newHtml = preg_replace($p, '$3', $html , PREG_SET_ORDER );

如果您的html被破坏,那么请使用htmlpurifier尝试清理它。PHP的dom非常挑剔,充其量只能吐出/吐出所有html,或者更糟。垃圾进来,垃圾出去,我不知道,谢谢。对于未关闭的标签,我将尝试使用它。但是我仍然需要取消图像的链接,我不想用正则表达式来表示HTML。
$p = "/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*<img.*)<\/a>/siU";
$newHtml = preg_replace($p, '$3', $html , PREG_SET_ORDER );
require_once 'library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.TidyLevel','heavy');
$config->set('AutoFormat.RemoveEmpty','true');
$config->set('AutoFormat.RemoveEmpty.RemoveNbsp','true');
$purifier = new HTMLPurifier($config);

$clean_html = $purifier->purify($content);
$html = new DOMDocument;
$html->preserveWhiteSpace = false;
$html->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$clean_html);
$as = $html->getElementsByTagName('a');
$ctr = $html->getElementsByTagName('a')->length;
for($i=$ctr;$i>0;--$i) {
    $a = $html->getElementsByTagName('a')->item($i-1);
    if($a->hasChildNodes()) {
        $img = $a->getElementsByTagName('img')->item(0);
        if($img != null) {
            $a->parentNode->replaceChild($img,$a);
        }
    }
}

foreach($html->getElementsByTagName('p') as $p) {
    $p->removeAttribute('style');
}
$text = $html->saveHTML();
echo $text;