<?php
/*----------------------------------------------文件夹与文件操作开始-----------------------------------------------*/
//读取文件
function read_file($l1){
return @file_get_contents($l1);
}
//写入文件
function write_file($l1, $l2=''){
$dir = dirname($l1);
if(!is_dir($dir)){
mkdirss($dir);
}
return @file_put_contents($l1, $l2);
}
//递归创建文件
function mkdirss($dirs,$mode=0777) {
if(!is_dir($dirs)){
mkdirss(dirname($dirs), $mode);
return @mkdir($dirs, $mode);
}
return true;
}
// 数组保存到文件
function arr2file($filename, $arr=''){
if(is_array($arr)){
$con = var_export($arr,true);
} else{
$con = $arr;
}
$con = "<?php\nreturn $con;\n?>";//\n!defined('IN_MP') && die();\nreturn $con;\n
write_file($filename, $con);
}
/*-------------------------------------------------系统路径相关函数开始------------------------------------------------------------------*/
//获取当前地址栏URL
function get_http_url(){
return htmlspecialchars("http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]);
}
//获取根目录路径
function get_site_path($filename){
$basepath = $_SERVER['PHP_SELF'];
$basepath = substr($basepath,0,strpos($basepath,$filename));
return $basepath;
}
//相对路径转绝对路径
function get_base_url($baseurl,$url){
if("#" == $url){
return "";
}elseif(FALSE !== stristr($url,"http://")){
return $url;
}elseif( "/" == substr($url,0,1) ){
$tmp = parse_url($baseurl);
return $tmp["scheme"]."://".$tmp["host"].$url;
}else{
$tmp = pathinfo($baseurl);
return $tmp["dirname"]."/".$url;
}
}
//获取指定地址的域名
function get_domain($url){
preg_match("|http://(.*)\/|isU", $url, $arr_domain);
return $arr_domain[1];
}
/*-------------------------------------------------字符串处理开始------------------------------------------------------------------*/
// UT*转GBK
function u2g($str){
return iconv("UTF-8","GBK",$str);
}
// GBK转UTF8
function g2u($str){
return iconv("GBK","UTF-8//ignore",$str);
}
// 去掉换行
function nr($str){
$str = str_replace(array("<nr/>","<rr/>"),array("\n","\r"),$str);
return trim($str);
}
// 去掉连续空白
function nb($str){
$str = str_replace(" ",' ',str_replace(" ",' ',$str));
$str = ereg_replace("[\r\n\t ]{1,}",' ',$str);
return trim($str);
}
// 转换成JS
function t2js($l1, $l2=1){
$I1 = str_replace(array("\r", "\n"), array('', '\n'), addslashes($l1));
return $l2 ? "document.write(\"$I1\");" : $I1;
}
// 计算中文字符串长度 与mb_strlen相同 中文与英文都统一为一个字节
function ff_mb_strlen($string = null) {
preg_match_all("/./us", $string, $match);
return count($match[0]);
}
// 字符串截取(同时去掉HTML与空白)
function msubstr($str, $start=0, $length, $suffix=false){
return ff_msubstr(eregi_replace('<[^>]+>','',ereg_replace("[\r\n\t ]{1,}",' ',nb($str))),$start,$length,'utf-8',$suffix);
}
function ff_msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){
if( function_exists('mb_strimwidth') ){
if($suffix){
return mb_strimwidth($str, $start, $length*2, '...', 'utf-8');
}
return mb_strimwidth($str, $start, $length*2, '', 'utf-8');
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$length_new = $length;
for($i=$start; $i<$length; $i++){
if (ord($match[0][$i]) > 0xa0){
//中文
}else{
$length_new++;
$length_chi++;
}
}
if($length_chi<$length){
$length_new = $length+($length_chi/2);
}
$slice = join("",array_slice($match[0], $start, $length_new));
if($suffix && $slice != $str){
return $slice."…";
}
return $slice;
}
// 汉字转拼单 $ishead 是否只返回首字母
function ff_pinyin($str, $ishead=0, $isclose=1){
$str = u2g($str);//转成GBK
global $pinyins;
$restr = '';
$str = trim($str);
$slen = strlen($str);
if($slen<2){
return $str;
}
if(count($pinyins)==0){
$fp = fopen('./Public/data/pinyin.dat','r');
while(!feof($fp)){
$line = trim(fgets($fp));
$pinyins[$line[0].$line[1]] = substr($line,3,strlen($line)-3);
}
fclose($fp);
}
for($i=0;$i<$slen;$i++){
if(ord($str[$i])>0x80){
$c = $str[$i].$str[$i+1];
$i++;
if(isset($pinyins[$c])){
if($ishead==0){
$restr .= $pinyins[$c];
}
else{
$restr .= $pinyins[$c][0];
}
}else{
//$restr .= "_";
}
}else if( eregi("[a-z0-9]",$str[$i]) ){
$restr .= $str[$i];
}
else{
//$restr .= "_";
}
}
if($isclose==0){
unset($pinyins);
}
return $restr;
}
// 生成字母前缀
function ff_url_letter($s0){
$firstchar_ord = ord(strtoupper($s0{0}));
if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)) return $s0{0};
$s=iconv("UTF-8","gb2312", $s0);
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319 and $asc<=-20284)return "A";
if($asc>=-20283 and $asc<=-19776)return "B";
if($asc>=-19775 and $asc<=-19219)return "C";
if($asc>=-19218 and $asc<=-18711)return "D";
if($asc>=-18710 and $asc<=-18527)return "E";
if($asc>=-18526 and $asc<=-18240)return "F";
if($asc>=-18239 and $asc<=-17923)return "G";
if($asc>=-17922 and $asc<=-17418)return "H";
if($asc>=-17417 and $asc<=-16475)return "J";
if($asc>=-16474 and $asc<=-16213)return "K";
if($asc>=-16212 and $asc<=-15641)return "L";
if($asc>=-15640 and $asc<=-15166)return "M";
if($asc>=-15165 and $asc<=-14923)return "N";
if($asc>=-14922 and $asc<=-14915)return "O";
if($asc>=-14914 and $asc<=-14631)return "P";
if($asc>=-14630 and $asc<=-14150)return "Q";
if($asc>=-14149 and $asc<=-14091)return "R";
if($asc>=-14090 and $asc<=-13319)return "S";
if($asc>=-13318 and $asc<=-12839)return "T";
if($asc>=-12838 and $asc<=-12557)return "W";
if($asc>=-12556 and $asc<=-11848)return "X";
if($asc>=-11847 and $asc<=-11056)return "Y";
if($asc>=-11055 and $asc<=-10247)return "Z";
return 0;//null
}
/*------------------------------------系统安全加密函数开始----------------------------------------------------*/
//加密
function ff_encrypt($data, $key){
$key = md5(empty($key) ? 'feifeicms' : $key);
$x = 0;
$len = strlen($data);
$l = strlen($key);
for ($i = 0; $i < $len; $i++)
{
if ($x == $l)
{
$x = 0;
}
$char .= $key{$x};
$x++;
}
for ($i = 0; $i < $len; $i++)
{
$str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
}
return base64_encode($str);
}
//解密
function ff_decrypt($data, $key){
$key = md5(empty($key) ? 'feifeicms' : $key);
$x = 0;
$data = base64_decode($data);
$len = strlen($data);
$l = strlen($key);
for ($i = 0; $i < $len; $i++)
{
if ($x == $l)
{
$x = 0;
}
$char .= substr($key, $x, 1);
$x++;
}
for ($i = 0; $i < $len; $i++)
{
if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1)))
{
$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
}
else
{
$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
}
}
return $str;
}
/*--------------------------------------------------系统常用快捷函数开始------------------------------------------*/
// 默认值处理
function ff_default($value, $default){
if($value){
return $value;
}
return $default;
}
// 逗号分隔的字符串去重 如模板栏目ID参数 $cids = array(1,2,3,...)
function ff_unique($cids){
$cidarr = explode(',',$cid);
$cidarr = array_unique($cidarr);
return $cidarr;
}
// 判断是否为windows/nginx 环境
function ff_isNginxWin(){
if(PATH_SEPAR
评论0