使用SimpleHtmlDom,如何删除和替换特定属性

发布于 2024-10-10 09:03:36 字数 317 浏览 2 评论 0原文

我目前正在使用 php 使用此 HTML DOM 解析器: http://simplehtmldom.sourceforge.net/

我'我对如何删除和替换所选属性 href="style.css" 感到困惑,我想用 "index/style.css" 替换链接,应该吗仅插入

索引/

或替换整个 html 代码中的整个属性?

I'm currently using this HTML DOM PARSER using php : http://simplehtmldom.sourceforge.net/

I'm confused on how to remove and replace the selected attribute href="style.css", I want to replace the link with "index/style.css", should I insert only the

index/

or replace the whole attribute from the whole html code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇柔作态 2024-10-17 09:03:36

这应该可以做到:

$doc = str_get_html($code);
foreach ($doc->find('a[href]') as $a) {
    $href = $a->href;
    if (/* $href begins with a relative URL path */) {
        $a->href = 'index/'.$href;
    }

}
$code = (string) $doc;

您还可以使用 PHP 的本机 DOM 库

$doc = new DOMDocument();
$doc->loadHTML($code);
$xpath = new DOMXpath($doc);
foreach ($xpath->query('//a[@href]') as $a) {
    $href = $a->getAttribute('href');
    if (/* $href begins with a relative URL path */) {
        $a->setAttribute('href', 'index/'.$href);
    }
}
$code = $doc->saveHTML();

This should do it:

$doc = str_get_html($code);
foreach ($doc->find('a[href]') as $a) {
    $href = $a->href;
    if (/* $href begins with a relative URL path */) {
        $a->href = 'index/'.$href;
    }

}
$code = (string) $doc;

You could also use PHP’s native DOM library:

$doc = new DOMDocument();
$doc->loadHTML($code);
$xpath = new DOMXpath($doc);
foreach ($xpath->query('//a[@href]') as $a) {
    $href = $a->getAttribute('href');
    if (/* $href begins with a relative URL path */) {
        $a->setAttribute('href', 'index/'.$href);
    }
}
$code = $doc->saveHTML();
划一舟意中人 2024-10-17 09:03:36

官方手册有几个示例,基本上涵盖了您需要的所有内容:

http://simplehtmldom.sourceforge.net/manual。 htm

如果您对某些特定步骤有疑问,请随时更新您的问题并提供一些代码。

The official manual has several examples that basically cover all you need:

http://simplehtmldom.sourceforge.net/manual.htm

If you have issues with some specific step, feel free to update your question and provide some of your code.

一抹淡然 2024-10-17 09:03:36
$html = str_get_html($string); 
if ($html){ // Verify connection, return False if could not load the resource
    $e = $html->find("a");
    foreach ($e as $e_element){
        $old_href = $e_element->outertext;
        // Do your modification in here 
        $e_element->href = affiliate($e_element->href); // for example I replace original link by the return of custom function named 'affiliate'
        $e_element->href = ""; //remove href
        $e_element->target .= "_blank"; // I added target _blank to open in new tab
        // end modification 
        $html = str_replace($old_href, $e_element->outertext, $html); // Update the href
    }
$html = str_get_html($string); 
if ($html){ // Verify connection, return False if could not load the resource
    $e = $html->find("a");
    foreach ($e as $e_element){
        $old_href = $e_element->outertext;
        // Do your modification in here 
        $e_element->href = affiliate($e_element->href); // for example I replace original link by the return of custom function named 'affiliate'
        $e_element->href = ""; //remove href
        $e_element->target .= "_blank"; // I added target _blank to open in new tab
        // end modification 
        $html = str_replace($old_href, $e_element->outertext, $html); // Update the href
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文