欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

获取新浪微博的微博秀内容存入数据表中

程序员文章站 2022-06-06 18:38:27
...
function BlogRequest($line){
	$line=str_replace("&",'&',$line);
	if(empty($line)){
		return false;
	}else{
		$subject = file_get_contents(trim($line));
		$regex = "/(([^<>]+))/ism";
		preg_match_all($regex,$subject,$blogArray);
		$i=3;
		$sqlValue=array();
		foreach($blogArray[0] as $k=>$v){
			$sqlValue[]=explode('',$blogArray[0][$i]);
			$i++;
		}
		foreach($sqlValue as $k=>$v){
			foreach($v as $key=>$value){
				$changeStr=$value;//内容不变真接存入数组
				if($key==1){//图片只需地址
					$changeStr=strip_tags(str_replace("alt=",'',preg_replace("/<img[^\\]?([^\\'\"\\s]*)['\"]?/is",'',$value)));
				}

				$minAgo=1;
				if($key==2){//在日期中分离一个链接地址出来
					$timeStr=explode('span',$value);
					$changeStr=str_replace('class="weiboShow_mainFeed_listContent_actionTime"','',$timeStr[1]);//发布时间
					if(substr_count($changeStr,'分钟前')>0){
						$oldmin=str_replace('分钟前','',trim(str_replace('>','',str_replace('"','',strip_tags($changeStr)))));
						$nowmin=date('i',time());
						$nowHore=date('H',time());
						if($oldmin>$nowmin){
							$fmin=$nowmin+60-$oldmin;
							$nowHore=$nowHore-1;
							$changeStr=date('Y-m-d ',time()).$nowHore.":".$fmin;
						}else{
							$fmin=$nowmin-$oldmin;
							$changeStr=date('Y-m-d ',time()).$nowHore.":".$fmin;
						}
						$minAgo=2;
					}else{
						if(substr_count($changeStr,'今天')>0){
							$changeStr=date('Y-m-d',time()).str_replace("今天",'',$changeStr);
						}else{
							$changeStr=date('Y-',time()).str_replace("日",' ',str_replace(" ",'',str_replace("月",'-',$changeStr)));
						};
					}
					//该博客链接 正则
					preg_match('/(?','',str_replace('"','',strip_tags($changeStr))));
					if($key==2){
						$sqlValue[$k][$key]=date('Y-m-d H:i',strtotime($sqlValue[$k][$key]));
					}
				}
			}
		}
		return $sqlValue;
	}
}