gpt4 book ai didi

php - 使用简单的 html dom 获取 url-data 属性

转载 作者:行者123 更新时间:2023-11-28 01:29:39 25 4
gpt4 key购买 nike

我关心的是使用 php html dom 库从 div 中提取链接。

代码示例:

include("simple_html_dom.php");
$html='
<div id="base" url-data="http://www.domaine.com/page?user=username"></div>
<div id="base" url-data="http://www.domaine.info/page?user=username"></div>
<div id="base" url-data="http://www.domaine.org/page?user=username"></div>
<div id="base" url-data="http://www.domaine.net/page?user=username"></div>
<div id="base" url-data="http://www.domaine.biz/page?user=username"></div>
<div id="base" url-data="http://www.domaine.fr/page?user=username"></div>
';

我需要从 div 中获取所有域名,然后使用简单的 html dom 解析器示例将它们存储在 php 数组中:

domaine.com,domaine.info,domaine.org,domaine.net,domaine.biz,domaine.fr

谢谢。

最佳答案

include("simple_html_dom.php");
$html='
<div id="base" url-data="http://www.domaine.com/page?user=username"></div>
<div id="base" url-data="http://www.domaine.info/page?user=username"></div>
<div id="base" url-data="http://www.domaine.org/page?user=username"></div>
<div id="base" url-data="http://www.domaine.net/page?user=username"></div>
<div id="base" url-data="http://www.domaine.biz/page?user=username"></div>
<div id="base" url-data="http://www.domaine.fr/page?user=username"></div>
';
$str_html=str_get_html($html);
// $file_html=file_get_html($html); // use file_get_html if you parse an url.
$div=$str_html->find("div#base");
$count=count($div)-1;
for($a=0;$a<=$count;$a++){
$url=$str_html->find("div#base",$a)->getAttribute('url-data');
$parse = parse_url($url);
$domain = $parse['host'];
$array[]=$domain;
}
print_r($array);

关于php - 使用简单的 html dom 获取 url-data 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30781047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com