public function fetchingData() {
header("Content-type:text/html;Charset=utf8");
$url="http://www.itheima.com.com/2017/0329/23.html"; //抓取網站內容
$ret=file_get_contents($url); // 將內容的編碼集由 GB312 替換成UTF-8
$ret=iconv('GB2312', 'UTF-8', $ret); // 替換掉不需要的內容
$ret=str_replace('', '', $ret); $ret =preg_replace("/(\d+)、/", "", $ret); //正則匹配內容數據
$pattern= "/\(.*)\<\/p\>/i"; preg_match_all($pattern, $ret,$matches); // 正則匹配標題數據
$titlePattern = "/\(.*)\<\/h2\>/i"; preg_match($titlePattern,$ret, $match);
$title =$match[1]; // 將匹配結果集放入數據庫
if(!empty($matches[1])) {
$filterResult = array_filter($matches[1]);
foreach($filterResult as $item) {
$data =[
'source' => 'http://www.itcast.com',
'author'=> $title,
'content' => $item,
'add_time' => time()
];
$id =$this->rhesis->addInfo($data);
if ($id > 0) {
echo$item.' add successful!'; echo "";
}
}
}
}