php采集页面指定标签里面的内容
2021-03-08 00:27
标签:== false sort file 标签 read turn 元素 foreach php采集页面指定标签里面的内容 标签:== false sort file 标签 read turn 元素 foreach 原文地址:https://www.cnblogs.com/zhaoying/p/12881155.html/*
* 参数说明: $tag_id:所要获取的元素Tag Id $url:所要获取页面的Url $tag:所要获取的标签 $data
*/
function getWebTag($tag_id, $url = false, $tag = ‘div‘, $data = false) {
if ($url !== false) {
$data = file_get_contents ( $url );
}
$charset_pos = stripos ( $data, ‘charset‘ );
if ($charset_pos) {
if (stripos ( $data, ‘utf-8‘, $charset_pos )) {
$data = iconv ( ‘utf-8‘, ‘utf-8‘, $data );
} else if (stripos ( $data, ‘gb2312‘, $charset_pos )) {
$data = iconv ( ‘gb2312‘, ‘utf-8‘, $data );
} else if (stripos ( $data, ‘gbk‘, $charset_pos )) {
$data = iconv ( ‘gbk‘, ‘utf-8‘, $data );
}
}
preg_match_all ( ‘/ $pre_div ) {
$divs [( int ) $pre_div [1]] = ‘p‘;
$divs [( int ) $suf_matches [0] [$index] [1]] = ‘s‘;
}
// 对div进行排序
$sort = array_keys ( $divs );
asort ( $sort );
$count = count ( $pre_matches [0] );
foreach ( $pre_matches [0] as $index => $pre_div ) {
//