PHP中通过preg_match_all函数获取页面信息并过滤变更为数组存储模式

// 1. 初始化
$ch = curl_init();

// 2. 设置选项
curl_setopt($ch, CURLOPT_URL, "http://test.com/index.jsp"); // 设置要抓取的页面地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 抓取结果直接返回(如果为0,则直接输出内容到页面)
curl_setopt($ch, CURLOPT_HEADER, 0); // 不需要页面的HTTP头
// 3. 执行并获取HTML文档内容,可用echo输出内容
$output = curl_exec($ch);
//dump($output);
$arr= array();
if(preg_match_all('/<tr>(.*?)</tr>/s', $output,$reg)){
foreach ($reg[1] as $t)
if(preg_match_all('/<div[^>]*>(.*?)</div>/',$t,$r))
$arr[]=$r[1];
}
foreach ($arr as $k => $v){
echo $v['3'].$v['4'].$v['18']."<br/>";
}
// print_r($arr);
// 4. 释放curl句柄
curl_close($ch);

  

原文地址:https://www.cnblogs.com/liuquan/p/php.html