<?php
class stole{
	public $content;
	public function __construct(){
		
	}
	public function getContent($url){
		$this->content=file_get_contents($url);
	}
	/**
	*正则匹配一条
	*/
	public function preg_one($content,$preg){
		$preg=$this->getPreg($preg); 
		preg_match("/$preg/is",$content,$a);
		if(isset($a[1])){
			return $a[1];
		}else{
			return false;
		}
	}
	/*获取匹配数据*/
	public function preg_data($content,$preg){
		$tags=$this->getTag($preg);
		$preg=$this->getPreg($preg);
		preg_match("/$preg/iUs",$content,$a);
		$arr=array();
		if($tags){
			foreach($tags as $k=>$v){
				$arr[$v]=isset($a[$k+1])?$a[$k+1]:array();
			}
		}
		return $arr;
	}
	/*
	*正则多条匹配
	*/
	public function preg_all($content,$preg){
		$tags=$this->getTag($preg);
		$preg=$this->getPreg($preg); 
		preg_match_all("/$preg/iUs",$content,$a);
		if($tags){
			foreach($tags as $k=>$v){
				$arr[$v]=isset($a[$k+1])?$a[$k+1]:array();
			}
		}
		return $arr;
	}
	/*
	*过滤正则
	*/
	public function getPreg($preg){
		$preg=str_replace("/","\/",$preg);
		//替换标题
		$preg=str_replace("{title}",".*",$preg);
		//替换url
		$preg=str_replace("{url}",".*",$preg);
		//替换时间
		$preg=str_replace("{time}",".*",$preg);
		//替换坐着
		$preg=str_replace("{author}",".*",$preg);
		//内容
		$preg=str_replace("{content}",".*",$preg);
		//简介
		$preg=str_replace("{description}",".*",$preg);
		return $preg;
	}
	/*获取解析标签*/
	public function getTag($preg){
		preg_match_all("/\({(.*)}\)/iUs",$preg,$a);
		if(isset($a[1])){
			return $a[1];
		}
	}
	/**
	*根据html标签来截取内容
	*/
	public function cutHtml($start,$content=""){
		$content=$content?$content:$this->content;
		$d=explode($start,$content);
		preg_match("/<(\w+)/is",$start,$h);
		$s_html="<{$h[1]}";
		$e_html="</{$h[1]}>";
		 
		$earr=explode($e_html,$d[1]);//截止
		$html=$temp=$earr[0];
		foreach($earr as $k=>$v){  
			if(strpos($temp,$s_html)!==false){  
				$temp=preg_replace("/{$s_html}/iUs","",$temp,1);
				$temp .=$earr[$k+1];
				if($k>0){
					$html.=$e_html.$v;
				}
			}else{
				if($k>0){
					$html.=$e_html.$v;
				}
				break;
			}
		}
		return $html;
	}
	/*
	*移除html
	*/
	public function removeHtml($start,$content=''){
		$content=$content?$content:$this->content;
		$html=$this->cutHtml($start,$content);
		return str_replace($html,"",$content);
		}
	}
?>