<?php
function geturlcontent($url,$charset){
	if(function_exists("file_get_contents")){
		for($i=0;$i<3;$i++){
			$content=@file_get_contents($url);
			if($content) break;
			}
		if(!IsNul($content) && function_exists('curl_init')){
			  $ch = curl_init();
			  $timeout = 5; // set to zero for no timeout
			  curl_setopt ($ch, CURLOPT_URL, $url);
			  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
			  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
			  $content = curl_exec($ch);
			  curl_close($ch);		
			}
		
		}
		
		
		if($charset=='UTF-8'){
				 $content=convert_encoding($content,'GBK','UTF-8');
			}	
			return $content;				  		
	}
	
//getlist 函数功能: 返回字符串中所有$startstr开始$endstr结束的字符串.
//返回结果 字符串数组
function getArr($startstr,$endstr,$content)
{	 
	  $startstr=ReplaceStr($startstr,'/','\/');
	  $endstr=ReplaceStr($endstr,'/','\/');
	  //$theregstr='/'.$startstr.'([\S]+?)'.$endstr.'/';
	  $theregstr='/'.$startstr.'(.*?)'.$endstr.'/';
	  preg_match_all($theregstr,$content,$match_array);
	  $newarray=$match_array[1];
	  return $newarray;
}
function getContent ($sourceStr,$star,$end,$flag )
{
  switch ($flag) {
    case 0:  //取指定字符前面的
        echo strrpos( $sourceStr, $end );
        echo '-----'.strlen( $end );
        $content = substr( $sourceStr, 0, strrpos( $sourceStr, $end ) + strlen( $end ) );
        break;
    case 1:  //取指定字符之间的,不包括指定字符
		if($star!="" && $end!=""){
        	$content = substr($sourceStr,strrpos($sourceStr,$star)+strlen($star));
        	//$content = substr( $content, 0, strrpos( $content, $end ) );
			$content = substr($content,0,strpos($content,$end));
			}
		else{
			$content="未知";
			}	
        break;
    case 2:  //取指定字符之间的，包括指定字符
        $content =strstr( $sourceStr, $star );

        $content = substr( $content, 0, strrpos( $content, $end ) + strlen( $end ) );
        break;
    case 3:  //取指定字符之后的，不包括指定字符
        $content = substr( $sourceStr, strrpos( $sourceStr,$star)+ strlen( $star ));
        break;
    case 4:  //取指定字符之后的，包括指定字符
        $content =strstr( $sourceStr, $star );
        break;
  }
    
        return $content;
}


function isdeal($str){
	set_magic_quotes_runtime(0);
	$magic= get_magic_quotes_gpc();	
	$str=$magic?$str:addslashes($str);
	return $str;
	}
//url补全函数	
function formaturl($l1,$l2){
if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i",$l1,$regs)){
      foreach($regs[0] as $num => $url){
       $l1 = str_replace($url,lIIIIl($url,$l2),$l1);
      }
}
return $l1;
}
function lIIIIl($l1,$l2){
if(preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i",$l1,$regs)){$I2 = $regs[3];}
if(strlen($I2)>0){
      $I1 = str_replace(chr(34),"",$I2);
      $I1 = str_replace(chr(39),"",$I1);
}else{return $l1;}
$url_parsed = parse_url($l2);
$scheme      = $url_parsed["scheme"];if($scheme!=""){$scheme = $scheme."://";}
$host      = $url_parsed["host"];
$l3       = $scheme.$host;
if(strlen($l3)==0){return $l1;}
if(!empty($url_parsed["path"])){
	$path = dirname($url_parsed["path"]);
}
else{
	$path="";
	}
if(!empty($path)&&$path[0]=="\\"){$path="";}
$pos      = strpos($I1,"#");
if($pos>0) $I1 = substr($I1,0,$pos);

//判断类型
if(preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&amp;)|&)+/i",$I1)){return $l1; }//http开头的url类型要跳过
elseif(!empty($I1)&&$I1[0]=="/"){$I1 = $l3.$I1;}//绝对路径
elseif(substr($I1,0,3)=="../"){//相对路径
      while(substr($I1,0,3)=="../"){
       $I1 = substr($I1,strlen($I1)-(strlen($I1)-3),strlen($I1)-3);
       if(strlen($path)>0){
        $path = dirname($path);
       }
      }
      $I1 = $l3.$path."/".$I1;
}
elseif(substr($I1,0,2)=="./"){
      $I1 = $l3.$path.substr($I1,strlen($I1)-(strlen($I1)-1),strlen($I1)-1);
}
elseif(strtolower(substr($I1,0,7))=="mailto:"||strtolower(substr($I1,0,11))=="javascript:"){
      return $l1;
}else{
      $I1 = $l3.$path."/".$I1;
}
return str_replace($I2,"\"$I1\"",$l1);
}

function CollectGet($key,$mode,$isfilter=''){
	set_magic_quotes_runtime(0);
	$magic= get_magic_quotes_gpc();	
	switch ($mode){
		case 'post':
			$value=isset($_POST[$key]) ? $magic?$_POST[$key]:addslashes($_POST[$key]) : '';
			break;
		case 'get':
			$value=isset($_GET[$key]) ? $magic?$_GET[$key]:addslashes($_GET[$key]) : '';
			break;
		default:
			$value=isset($_POST[$key]) ? $magic?$_POST[$key]:addslashes($_POST[$key]) : '';
			if($value==""){
				$value=isset($_GET[$key]) ? $magic?$_GET[$key]:addslashes($_GET[$key]) : '';
				}
			break;
		}
	if($isfilter!=''){
		$value=str_encode($value);
		}
	
	return $value;	
}

function collectRep($str,$reg){
		if(IsNul($reg)){
		$regArr=explode("\n",$reg);
		for($i=0;$i<count($regArr);$i++){
			$restr=explode('@@',$regArr[$i]);
			$ostr=$restr[0];
			$nstr=$restr[1];
			$str=ReplaceStr($str,$ostr,$nstr);
			}
		}
		return $str;
		
	}

function removeFilter($str,$safe){
		if(IsNul($safe)){
			$safeArr=explode(',',$safe);
			for($i=0;$i<count($safeArr);$i++){
				if(strtolower($safeArr[$i])=='iframe' or strtolower($safeArr[$i])=='object' or strtolower($safeArr[$i])=='script' or strtolower($safeArr[$i])=='embed'){
					$regEx="/<$safeArr[$i].*?>(.*?)</$safeArr[$i]>/";
					}
				elseif(strtolower($safeArr[$i])!='a'){
					$regEx="/<$safeArr[$i].*?>(.*?)</$safeArr[$i].*?>/" ;
					}
				elseif(strtolower($safeArr[$i])=='a'){
					$regEx="/<a.+?>(.*?)<\/a>/" ;
					}
				elseif(strtolower($safeArr[$i])=='img'){
					$regEx="/<img[\S\s]+?src=([\S]*).+?>/";
					}
			if(IsNul($regEx)){		
				preg_match_all($regEx,$str,$pregArr);
				if(!empty($pregArr)){
					$str=ReplaceStr($str,$pregArr[0],$pregArr[1]);
					}
				}
			}			
		}
	return $str;	
	}
	
function dealPic($pic,$weburl,$content=''){
		
		if(stristr($pic,"http://")==false){
			$pic=$weburl.$pic;
			}
		$ext=strtolower(strrchr($pic,".")); 
		if($ext!=".gif" && $ext!=".jpg" && $ext!=".png" && $ext!=".bmp" && $ext!=".jpeg"){
				$reg="/<img[^>]+src=\"([^\"]+)\"[^>]*>|<img[^>]+src='([^']+)'[^>]*>/";
				preg_match($reg,$content,$arr);
				if(!empty($arr)){
					$pic=$arr[1];
					}
				else{
					$pic="图片未知";
					}	
		} 
		return $pic;	
	}	
?>