我有这样的html.我想比较锚标记(href)和img src.如果href和src是相同的,那么它不应该超链接


I have html like this.I want to compare anchor tag(href) and img src.If both href and src are same then it should not hyperlink.

<a href="http://dev.tone.com/wp-content/uploads/2014/07/israel-gaza-conflict-14.jpg">
<img class="size-medium wp-image-2820822" src="http://dev.tone.com/wp-
content/uploads/2014/07/israel-gaza-conflict-14.jpg" alt="ed over the Gaza Strip" width="300" 
height="200" /></a>
<a href="http://google.com"><img class="size-medium wp-image-2820817" 
src="http://dev.time.com/wp-content/uploads/2014/07/what-makes-people-happy1.jpg" 
alt="183023480" width="300" height="200" /></a>

如果href和src是相同的,那么它不应该超链接。如何使用php

使用PHP的DOMDocument和DOMXPath,这很容易实现。

$doc = new DOMDocument();
$doc->loadHTML($html);
$dox = new DOMXPath($doc);
$anchors = $dox->query("//a[./img]");
foreach($anchors as $anchor){
    $href = $anchor->getAttribute("href");
    $src = $anchor->getElementsByTagName("img")->item(0)->getAttribute("src");
    echo $href==$src? "equal": "not equal";
    echo "'n";
}

小提琴