<?php 
//过滤字符串中,不安全因素

function funstr($str){
	
	$str=mysql_real_escape_string($str);
	//$str=str_replace(">","",$str);
	//$str=str_replace("=","",$str);
	//$str=str_replace("'","",$str);
	//$str=str_replace("","",$str);
	$str=trim($str);
	return $str;
}

function funstr_1($str){
  $str=str_replace("\\","",$str);
  return $str;
}

//获取中间内容
function midstr($str,$str1,$str2){
  $arr1=explode($str1,$str);
  if(count($arr1)==1){
    $mid=0;
  }
  else{
    $arr2=explode($str2,$arr1[1]);
	if(count($arr2)==1){$mid=0;}else{$mid=$arr2[0];}
  }
  return $mid;
}

//检查文件夹是否存在,不存在则创建
//函数名:createdir($dir)
//$dir,文件夹名称
//无返回值
function createdir($dir){
    if (!is_dir($dir)){
	  mkdir($dir,0777);
	}
}
//不检查文件直接创建文件
//函数名:createfile($file,$str)
//$file 文件地址 $str 文件内容
//无返回值
function createfile($file,$str){
  $fp=fopen($file,"w");
  fwrite($fp,$str);
}
//读文件 openfile()
function openfile($url){
  $str="";
  $i=0;
  $f=@file($url);
  while($i<count($f)){
    $str=$str.$f[$i];
	$i=$i+1;
  }
  return $str;
}
//输出风格名
function temp(){
  $str="";
  $dir="../temp/";
  $handle=opendir($dir);
  while($read=readdir($handle)){
    if ($read=='.'){
	}
	elseif($read=='..'){
	}
	else{
	  $str=$str."<option value=".$read.">".$read."</option>";
	}
  }
  return $str;
}
//弹出对话框提示函数
//函数命名:tanshow($str,$url)
//$str,提示字符.$url,转向网站!
function tanshow($str="",$url=""){
  if($str==""){
    return "<script>window.document.location.href='$url';</script>";
  }
  if ($url==""){
    return "<script>alert('$str');history.go(-1);</script>";
  }
  else{
    return "<script>alert('$str');window.document.location.href='$url';</script>";
  }
}
//去htm标记函数
//例:html_tags(array("a","img"),$str); 
function html_clear($tags_a,$str) 
{ 
    foreach ($tags_a as $tag) 
    { 
        $p[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i"; 
    } 
    $return_str = preg_replace($p,"",$str); 
    return $return_str; 
} 
//html_clears($str)
function html_clears($str){
  $strs=html_clear(array("a","object","marquee","param","embed","table","tr","th","tbody","li","span","div","td","script","font","strong","b","!","h2","h1","center"),$str);
  $strs=trim($strs);
  return $strs; 
} 

//获取字符长度strlen_utf8
function utf_strlen($str) {
  $i = 0;
  $count = 0;
  $len = strlen ($str);
  while ($i < $len) {
    $chr = ord ($str[$i]);
    $count++;
    $i++;
    if($i >= $len) break;
    if($chr & 0x80) {
      $chr <<= 1;
      while ($chr & 0x80) {
        $i++;
        $chr <<= 1;
      }
    }
  }
  return $count;
}
//截取字符串 utf-8
function utf_substr($str,$len) 
{ 
  for($i=0;$i<$len;$i++) { 
    $temp_str=substr($str,0,1); 
    if(ord($temp_str) > 127) { 
      $i++; 
      if($i<$len) { 
        $new_str[]=substr($str,0,3); 
        $str=substr($str,3); 
      } 
     } 
     else { 
        $new_str[]=substr($str,0,1); 
        $str=substr($str,1); 
     } 
  } 
  return join($new_str); 
} 

//遍历文件夹文件
function list_files( $folder = '', $levels = 100 ) { 
    if( empty($folder) ) 
        return false; 

    if( ! $levels ) 
        return false; 

    $files = array(); 
    if ( $dir = @opendir( $folder ) ) { 
        while (($file = readdir( $dir ) ) !== false ) { 
            if ( in_array($file, array('.', '..') ) ) 
                continue; 
            if ( is_dir( $folder . '/' . $file ) ) { 
                $files2 = list_files( $folder . '/' . $file, $levels - 1); 
                if( $files2 ) 
                    $files = array_merge($files, $files2 ); 
                else 
                    $files[] = $folder . '/' . $file . '/'; 
            } else { 
                $files[] = $folder . '/' . $file; 
            } 
        } 
    } 
    @closedir( $dir ); 
    return $files; 
}
//目录函数 0输出目录下文件,1输出目录下文件夹,2全部输出
function listdir($dir,$num){
  $str='';
  if(is_dir($dir)){
    $path=@opendir($dir);
	while(($files=readdir($path))!==false){
	  if($files=='.'){continue;}
	  if($files=='..'){continue;}
	  if($num==0){if((filetype($dir.'/'.$files))<>'dir'){$str.=$files.',,';}}
	  if($num==1){if((filetype($dir.'/'.$files))=='dir'){$str.=$files.',,';}}
	  if($num==2){$str.=$files.',,';}
	}
	@closedir( $dir ); 
  }
  return $str;
}

//获取当前登陆用户IP
function get_client_ip()
{
  if ($_SERVER['REMOTE_ADDR']) {
    $cip = $_SERVER['REMOTE_ADDR'];
  } elseif (getenv("REMOTE_ADDR")) {
    $cip = getenv("REMOTE_ADDR");
  } elseif (getenv("HTTP_CLIENT_IP")) {
    $cip = getenv("HTTP_CLIENT_IP");
  } else {
    $cip = "unknown";
  }
return $cip;
}

?>