简单的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; } } }更多推荐
发布评论