简单的HTML Dom

编程入门 行业动态 更新时间:2024-10-18 06:13:42
简单的HTML Dom - 替换相对链接(Simple HTML Dom - Replace Relative Links)

我正在使用simple_html_dom库来抓取外部网站的菜单。

这工作正常,但链接都是相对的,返回/关于/联系,而不是mydomain.com/about/contact。

如何在循环中替换这些实例的最佳方法? 例如:

$html_out = file_get_html('http://www.domain.com/'); if ( $html_out ) { foreach($html_out->find('#menu-position-wrapper') as $article) { echo $article; }

谢谢!

I am using the simple_html_dom libary to scrape a menu of an external site.

This works fine, but the links are all relative and return /about/contact rather then mydomain.com/about/contact.

How is the best way to replace these instances within my loop? Ex:

$html_out = file_get_html('http://www.domain.com/'); if ( $html_out ) { foreach($html_out->find('#menu-position-wrapper') as $article) { echo $article; }

Thanks!

最满意答案

你可以试试这个:

$html_out = file_get_html('http://www.domain.com/'); if ( $html_out ) { foreach($html_out->find('#menu-position-wrapper') as $article) { foreach ($article->find('a[href]') as $a) { $href = $a->href; $a->href = 'http://www.domain.com'.$href; } } }

You can try this:

$html_out = file_get_html('http://www.domain.com/'); if ( $html_out ) { foreach($html_out->find('#menu-position-wrapper') as $article) { foreach ($article->find('a[href]') as $a) { $href = $a->href; $a->href = 'http://www.domain.com'.$href; } } }

更多推荐

本文发布于:2023-08-07 06:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简单   HTML   Dom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!