MarkJane
4/18/2017 - 2:42 AM

finecms中摘取的common函数.php

finecms中摘取的common函数.php

/**
 * 字符截取 支持UTF8/GBK
 */
function strcut($string, $length, $dot = '...') {
    $charset = 'utf-8';
	if (strlen($string) <= $length) return $string;
	$string  = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
	$strcut  = '';
	if (strtolower($charset) == 'utf-8') {
		$n   = $tn = $noc = 0;
		while ($n < strlen($string)) {
			$t = ord($string[$n]);
			if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1; $n++; $noc++;
			} elseif (194 <= $t && $t <= 223) {
				$tn = 2; $n += 2; $noc += 2;
			} elseif (224 <= $t && $t <= 239) {
				$tn = 3; $n += 3; $noc += 2;
			} elseif (240 <= $t && $t <= 247) {
				$tn = 4; $n += 4; $noc += 2;
			} elseif (248 <= $t && $t <= 251) {
				$tn = 5; $n += 5; $noc += 2;
			} elseif ($t == 252 || $t == 253) {
				$tn = 6; $n += 6; $noc += 2;
			} else {
				$n++;
			}
			if($noc >= $length) break;
		}
		if ($noc > $length) $n -= $tn;
		$strcut = substr($string, 0, $n);
	} else {
		for ($i = 0; $i < $length; $i++) {
			$strcut .= ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];
		}
	}
	$strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
	return $strcut . $dot;
}

/**
 * 清除HTML标记
 */
function clearhtml($str) {
    $str = str_replace(array('&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array(' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $str);
    $str = preg_replace("/\<[a-z]+(.*)\>/iU", "", $str);
    $str = preg_replace("/\<\/[a-z]+\>/iU", "", $str);
    $str = str_replace(array(' ','	', chr(13), chr(10), '&nbsp;'), array('', '', '', '', ''), $str);
    return $str;
}


/**
 * 返回经addslashes处理过的字符串或数组
 * @param $string 需要处理的字符串或数组
 * @return mixed
 */
function new_addslashes($string){
	if(!is_array($string)) return addslashes($string);
	foreach($string as $key => $val) $string[$key] = new_addslashes($val);
	return $string;
}

/**
 * 返回经stripslashes处理过的字符串或数组
 * @param $string 需要处理的字符串或数组
 * @return mixed
 */
function new_stripslashes($string) {
	if(!is_array($string)) return stripslashes($string);
	foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
	return $string;
}

/**
 * 返回经addslashe处理过的字符串或数组
 * @param $obj 需要处理的字符串或数组
 * @return mixed
 */
function new_html_special_chars($string) {
	if(!is_array($string)) return htmlspecialchars($string);
	foreach($string as $key => $val) $string[$key] = new_html_special_chars($val);
	return $string;
}

/**
 * 安全过滤函数
 * @param $string
 * @return string
 */
function safe_replace($string) {
	$string = str_replace('%20','',$string);
	$string = str_replace('%27','',$string);
	$string = str_replace('%2527','',$string);
	$string = str_replace('*','',$string);
	$string = str_replace('"','&quot;',$string);
	$string = str_replace("'",'',$string);
	$string = str_replace('"','',$string);
	$string = str_replace(';','',$string);
	$string = str_replace('<','&lt;',$string);
	$string = str_replace('>','&gt;',$string);
	$string = str_replace("{",'',$string);
	$string = str_replace('}','',$string);
	return $string;
}

/**
* 将字符串转换为数组
* @param	string	$data	字符串
* @return	array	返回数组格式,如果,data为空,则返回空数组
*/
function string2array($data) {
	if ($data == '') return array();
	if (is_array($data)) return $data;
	if (strpos($data, 'array') !== false && strpos($data, 'array') === 0) {
	    @eval("\$array = $data;");
		return $array;
	}
	return unserialize($data);
}

/**
* 将数组转换为字符串
* @param	array	$data		数组
* @param	bool	$isformdata	如果为0,则不使用new_stripslashes处理,可选参数,默认为1
* @return	string	返回字符串,如果,data为空,则返回空
*/
function array2string($data, $isformdata = 1) {
	if($data == '') return '';
	if($isformdata) $data = new_stripslashes($data);
	return serialize($data);
}

/**
 * 编码转换函数
 * @param  $str 
 * @param  $from 
 * @param  $to
 * @return string
 */
function convert($str, $from = 'gbk', $to = 'utf-8') {
	if (!$str) return '';
	if (strtolower($from) == strtolower($to)) return $str;
	$to   = str_replace('gb2312', 'gbk', strtolower($to));
	$from = str_replace('gb2312', 'gbk', strtolower($from));
	if ($form == 'gbk' && $to == 'utf-8') {
	    return gbk_to_utf8($str);
	} elseif ($form == 'utf-8' && $to == 'gbk') {
	    return utf8_to_gbk($str);
	} else {
	    return $str;
	}
}

/**
 * utf8转gbk
 * @param $utfstr
 */
function utf8_to_gbk($utfstr) {
	$filename	= EXTENSION_DIR . 'encoding' . DIRECTORY_SEPARATOR . 'gb-unicode.table';
	$UC2GBTABLE = array();
	$fp	= fopen($filename, 'rb');
	while($l = fgets($fp, 15)) {        
		$UC2GBTABLE[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));
	}
	fclose($fp);
	$okstr = '';
	$ulen  = strlen($utfstr);
	for($i=0; $i<$ulen; $i++) {
		$c  = $utfstr[$i];
		$cb = decbin(ord($utfstr[$i]));
		if(strlen($cb)==8) { 
			$csize = strpos(decbin(ord($cb)), '0');
			for($j = 0; $j < $csize; $j++) {
				$i++; 
				$c .= $utfstr[$i];
			}
			$c = utf8_to_unicode($c);
			if(isset($UC2GBTABLE[$c])) {
				$c = dechex($UC2GBTABLE[$c]+0x8080);
				$okstr .= chr(hexdec($c[0] . $c[1])) . chr(hexdec($c[2] . $c[3]));
			} else {
				$okstr .= '&#' . $c . ';';
			}
		} else {
			$okstr .= $c;
		}
	}
	$okstr = trim($okstr);
	return $okstr;
}

/**
 * gbk转utf8
 * @param $gbstr
 */
function gbk_to_utf8($gbstr) {
	$filename  = EXTENSION_DIR . 'encoding' . DIRECTORY_SEPARATOR . 'gb-unicode.table';
	$CODETABLE = array();
	$fp	= fopen($filename, 'rb');
	while ($l = fgets($fp, 15)) { 
		$CODETABLE[hexdec(substr($l, 0, 6))] = substr($l, 7, 6); 
	}
	fclose($fp);
	$ret  = '';
	$utf8 = '';
	while ($gbstr) {
		if (ord(substr($gbstr, 0, 1)) > 0x80) {
			$thisW = substr($gbstr, 0, 2);
			$gbstr = substr($gbstr, 2, strlen($gbstr));
			$utf8 = '';
			@$utf8 = unicode_to_utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW)) - 0x8080]));
			if ($utf8 != '') {
				for ($i = 0; $i < strlen($utf8); $i += 3) $ret .= chr(substr($utf8, $i, 3));
			}
		} else {
			$ret .= substr($gbstr, 0, 1);
			$gbstr = substr($gbstr, 1, strlen($gbstr));
		}
	}
	return $ret;
}

/**
 * unicode转utf8
 * @param  $c
 */
function unicode_to_utf8($c) {
	$str = '';
	if ($c < 0x80) {
		$str .= $c;
	} elseif($c < 0x800) {
		$str .= (0xC0 | $c >> 6);
		$str .= (0x80 | $c & 0x3F);
	} elseif($c < 0x10000) {
		$str .= (0xE0 | $c >> 12);
		$str .= (0x80 | $c >> 6 & 0x3F);
		$str .= (0x80 | $c & 0x3F);
	} elseif($c < 0x200000) {
		$str .= (0xF0 | $c >> 18);
		$str .= (0x80 | $c >> 12 & 0x3F);
		$str .= (0x80 | $c >> 6 & 0x3F);
		$str .= (0x80 | $c & 0x3F);
	}
	return $str;
}

/**
 * utf8转unicode
 * @param  $c
 */
function utf8_to_unicode($c) {
	switch (strlen($c)) {
		case 1:
		  return ord($c);
		case 2:
		  $n = (ord($c[0]) & 0x3f) << 6;
		  $n += ord($c[1]) & 0x3f;
		  return $n;
		case 3:
		  $n = (ord($c[0]) & 0x1f) << 12;
		  $n += (ord($c[1]) & 0x3f) << 6;
		  $n += ord($c[2]) & 0x3f;
		  return $n;
		case 4:
		  $n = (ord($c[0]) & 0x0f) << 18;
		  $n += (ord($c[1]) & 0x3f) << 12;
		  $n += (ord($c[2]) & 0x3f) << 6;
		  $n += ord($c[3]) & 0x3f;
		  return $n;
	}
}

/**
 * 格式化输出文件大小
 */
function formatFileSize($fileSize, $round = 2) {
    if (empty($fileSize)) return 0;
	$i    = 0;
	$inv  = 1 / 1024;
	$unit = array(' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB');
	while ($fileSize >= 1024 && $i < 8) {
		$fileSize *= $inv;
		++$i;
	}
	$fileSizeTmp = sprintf("%.2f", $fileSize);
	$value = $fileSizeTmp - (int)$fileSizeTmp ? $fileSizeTmp : $fileSize;
	return round($value, $round) . $unit[$i];
}

/**
 * 统计目录大小
 */
function count_dir_size($dir) {
    if (!is_dir($dir)) return 0;
	set_time_limit(0);
	$count  = 0;
    $handle = opendir($dir);
	while (false !== ($file = readdir($handle))) {
        if ($file == '.' || $file == '..') continue;
		$path = $dir . $file;
		if (is_dir($path)) {
			$count += count_dir_size($path . '/', $size);
		} elseif (is_file($path)) {
			$count += filesize($path);
		}
	}
	closedir($handle);
	return $count;
}
/**
 * 调用远程数据
 */
function fn_geturl($url) {
    if (substr($url, 0, 7) != 'http://') return file_get_contents($url);
    if (ini_get('allow_url_fopen')) {
	    return @file_get_contents($url);
	} elseif (function_exists('curl_init') && function_exists('curl_exec')) {
	    $ch   = curl_init($url);
	    $data = '';
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
}
/**
 * 格式SQL查询IN(ID序列)
 * @param  $str
 * @param  $glue
 * @return boolean|string
 */
function formatStr($str, $glue = ',') {
    $arr = explode($glue, $str);
    if (!is_array($arr)) return false;
    $arr = array_unique($arr);
    $ids = '';
    foreach ($arr as $id) { if ($id) $ids .= ',' . $id; }
    return substr($ids, 1);
}