dongzha0813 2013-09-25 00:35 采纳率: 0%
浏览 29
已采纳

用php简单的html dom替换纯文本链接

I have a program that removes certain pages from a web; i want to then traverse the remaining pages and "unlink" any links to those removed pages. I'm using simplehtmldom. My function takes a source page ($source) and an array of pages ($skipList). It finds the links, and I'd like to then manipulate the dom to convert the element into the $link->innertext, but I don't know how. Any help?

function RemoveSpecificLinks($source, $skipList) {
    // $source is the html source file; 
    // $skipList is an array of link destinations (hrefs) that we want unlinked
$docHtml    = file_get_contents($source);
$htmlObj    = str_get_html($docHtml);
$links  = $htmlObj->find('a');
if (isset($links)) {
    foreach ($links as $link) {
        if (in_array($link->href, $skipList)) {
            $link->href = ''; // Should convert to simple text element
        }
    }
}
$docHtml    = $htmlObj->save(); 
$htmlObj->clear();
unset($htmlObj);
return($docHtml);
}
  • 写回答

1条回答 默认 最新

  • douhui1333 2013-09-25 00:47
    关注

    I have never used simplehtmldom, but this is what I think should solve your problem:

    function RemoveSpecificLinks($source, $skipList) {
        // $source is the HTML source file; 
        // $skipList is an array of link destinations (hrefs) that we want unlinked
    $docHtml    = file_get_contents($source);
    $htmlObj    = str_get_html($docHtml);
    $links  = $htmlObj->find('a');
    if (isset($links)) {
        foreach ($links as $link) {
            if (in_array($link->href, $skipList)) {
    
                $link->outertext = $link->plaintext; // THIS SHOULD WORK
    
                // IF THIS DOES NOT WORK TRY:
                // $link->outertext = $link->innertext;
            }
        }
    }
    $docHtml    = $htmlObj->save(); 
    $htmlObj->clear();
    unset($htmlObj);
    return($docHtml);
    }
    

    Please provide me some feedback as if this worked or not, also specifying which method worked, if any.

    Update: Maybe you would prefer this:

    $link->outertext = $link->href;
    

    This way you get the link displayed, but not clickable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端页面想做个定时任务,但是使用requestAnimationFrame,setinterval和settimeout都不行
  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?