我想要刪除所有與此域vnexpress.net
相匹配的鏈接。href
屬性。 這是一個鏈接例如:如何在php中使用正則表達式刪除鏈接?
<a href="http://vnexpress.net/whatever">whatever</a>
這是我的代碼:
$contents = preg_replace('/<a\s*href=\"*vnexpress*\"\s(.*)>(.*)<\/a>/', '', $data->content);
請幫幫我!非常感謝!。
我想要刪除所有與此域vnexpress.net
相匹配的鏈接。href
屬性。 這是一個鏈接例如:如何在php中使用正則表達式刪除鏈接?
<a href="http://vnexpress.net/whatever">whatever</a>
這是我的代碼:
$contents = preg_replace('/<a\s*href=\"*vnexpress*\"\s(.*)>(.*)<\/a>/', '', $data->content);
請幫幫我!非常感謝!。
您已經在這裏要求提供正則表達式,但解析HTML不是正確的tool。
$doc = new DOMDocument;
$doc->loadHTML($html); // load the html
$xpath = new DOMXPath($doc);
$links = $xpath->query("//a[contains(@href, 'vnexpress.net')]");
foreach ($links as $link) {
$link->parentNode->removeChild($link);
}
echo $doc->saveHTML();
試試這個:
$re = "/<a[^>]+href=\"[^\"]*vnexpress.net[^>]+>(.*)<\\/a>/m";
$str = "<a id=\"\" href=\"http://vnexpress.net/whatever\">whatever <b>sss</b> </a>\n<a id=\"\" href=\"http://new.net/whatever\">whatever</a>\n";
$subst = "$1";
$result = preg_replace($re, $subst, $str);
什麼正則表達式你有那麼遠,並給它什麼樣的錯誤? – Nerixel
'href =「\ K [^」] * \ bvnexpress \ .net [^「] *' –
@AvinashRaj我嘗試使用您的代碼,但它不起作用 –