1

我抓取了一个 html 代码并将其插入到我的数据库中。

它看起来像这样,但我并不完全需要这个。我需要相同的 html 代码,但我希望其中的所有链接都具有 href="#" 并且我不知道该怎么做。

<div id="olpDivId"> <span class="olpCondLink" ><a class="buyAction olpBlueLink" href="/gp/offer-listing/B002YDZSQM/ref=dp_olp_new/192-2317768-5155903?ie=UTF8&condition=new">4&nbsp;new</a>&nbsp;from&nbsp;<span class="price">$164.99</span></span> <span class="olpCondLink" ><a class="buyAction olpBlueLink" href="/gp/offer-listing/B002YDZSQM/ref=dp_olp_used/192-2317768-5155903?ie=UTF8&condition=used">18&nbsp;used</a>&nbsp;from&nbsp;<span class="price">$80.00</span></span> <span class="olpCondLink" ><a class="buyAction olpBlueLink" href="/gp/offer-listing/B002YDZSQM/ref=dp_olp_refurbished/192-2317768-5155903?ie=UTF8&condition=refurbished">6&nbsp;refurbished</a>&nbsp;from&nbsp;<span class="price">$124.95</span></span> </div>

我是否清楚自己要做什么?

最好的祝福。

4

1 回答 1

1

您可以使用domDocument 来解析 HTML 并对其进行编辑:

<?php

    $html='<div id="olpDivId"> <span class="olpCondLink" ><a class="buyAction olpBlueLink" href="/gp/offer-listing/B002YDZSQM/ref=dp_olp_new/192-2317768-5155903?ie=UTF8&condition=new">4&nbsp;new</a>&nbsp;from&nbsp;<span class="price">$164.99</span></span> <span class="olpCondLink" ><a class="buyAction olpBlueLink" href="/gp/offer-listing/B002YDZSQM/ref=dp_olp_used/192-2317768-5155903?ie=UTF8&condition=used">18&nbsp;used</a>&nbsp;from&nbsp;<span class="price">$80.00</span></span> <span class="olpCondLink" ><a class="buyAction olpBlueLink" href="/gp/offer-listing/B002YDZSQM/ref=dp_olp_refurbished/192-2317768-5155903?ie=UTF8&condition=refurbished">6&nbsp;refurbished</a>&nbsp;from&nbsp;<span class="price">$124.95</span></span> </div>';
    $dom = new domDocument;
    $dom->loadHTML(  $html);
    $dom->preserveWhiteSpace = false;
    $links = $dom->getElementsByTagName("a");
    foreach($links as $link){
        $link->setAttribute('href','#');
    }
    echo $dom->saveHTML();

?>
于 2013-08-28T21:04:21.670 回答