2011-08-18 34 views
0

我想删除网页上的如何从网页中删除连续的链接?

这里连续的链接是一个示例

<div style="font-family: Arial;"> 
    <br> 
    &nbsp; 
    <a href="http://google.com">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</a> 
    &nbsp; 
    <a href="http://google.com">BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</a> 
    Google is a search 
    <a href="http://www.google.com">engine</a> 

在上面的HTML我想删除第2个所述的标签,而不是第三个(我的剧本只应删除连续标签)

+6

太棒了!你有什么尝试?你为什么认为这个正则表达式会为你解决这个问题?这是一个实际的要求,还是你会从标签中删除它? –

+0

嘿,这个问题一直在编辑!这不是我要求的! – Shan

+0

@Shan你如何定义“连续的标签”?用空格和/或' '分隔? – Phil

回答

2

不要使用这个正则表达式。它们非常强大,但不适合寻找这种“连续”标签。我建议你使用DOM。然后你可以浏览HTML树。 这里是一个例子(未测试):

$doc = new DOMDocument(); 
// avoid blank nodes when parsing 
$doc->preserveWhiteSpace = false; 
// reads HTML in a string, loadHtmlFile() also exists 
$doc->loadHTML($html); 
// find all "a" tags 
$links = $doc->getElementsByTagName('a'); 
// remove the first link 
$parent = $links->item(0)->parentNode; 
$parent->removeChild($links->item(0)); 
// test the node following the second link 
if ($links->item(1)->nextSibling->nodeType != XML_TEXT_NODE) { 
    // delete this node ... 
} 
// print the modified HTML 
// See DOMDocument's attributes if you want to format the output 
echo $doc->saveHTML(); 
相关问题