<?
//字符串标记转换
function str_format($str)
{
$str = str_replace ("&","&",$str);
$str = str_replace("'",''',$str);
$str = str_replace('"','"',$str);
$str = str_replace('\\','\',$str);
$str = str_replace ("<","<",$str);
$str = str_replace (">",">",$str);
return $str;
}
//特殊符号过滤,主要用于表单提交的数据
function frm_str_format($str)
{
$str = str_replace(chr(92).chr(39),"'",$str); //过滤单引号\'
$str = str_replace(chr(39),"'",$str); //过滤单引号\'
$str = str_replace(chr(92).chr(34),""",$str); //过滤双引号\"
$str = str_replace(chr(34),""",$str); //过滤双引号
$str = str_replace(chr(92).chr(92),"\",$str); //过滤反斜杆\,form表单提交时会将\转换为\\,所以要将其还原为\
$str = str_replace(chr(92),"\",$str); //过滤反斜杆\
return $str;
}
//将GB2312转换为UTF-8编码, 发现有的服务器不支持iconv这个函数
function code_u($str)
{
$str=iconv("GB2312//TRANSLIT","UTF-8",$str);
return $str;
}
//将UTF-8转换为GB2312编码
function code_g($str)
{
$str=iconv("UTF-8","GB2312//TRANSLIT",$str);
return $str;
}
//弹出对话框
function alert($str)
{
echo "<script>alert('$str');</script>";
}
//返回上一步
function goback()
{
echo "<script>history.go(-1);</script>";
}
//页面跳转
function location($url)
{
echo "<script>location.href='$url';</script>";//客户端跳转
//header("location:$url"); //PHP服务器端跳转
}
//无提示关闭窗口
function window_close()
{
echo "<script>window.opener=null;window.close();</script>";
}
//返回日期时间值
function now()
{
return date( "Y-m-d H:i:s");
}
//从左边截取$num个字节
function left($str,$num)
{
$value="";
for($i = 0; $i <$num; $i++){
if(ord(substr(trim($str), $i, 1)) > 0xa0){
if ($i<>$num-1){
$value .= substr(trim($str), $i, 2);
$i++;
}
}else{
$value .= substr(trim($str), $i, 1);
}
}
return $value;
}
//从左边截取$num个字符
function left_var($str,$num)
{
$value="";
for($i = 0; $i <$num; $i++){
if(ord(substr(trim($str), $i, 1)) > 0xa0){
$value .= substr(trim($str), $i, 2);
$i++;
$num++;
}else{
$value .= substr(trim($str), $i, 1);
}
}
return $value;
}
//从右边截取$num个字符
function right_var($str,$num)
{
//本函数还未实现
return $str;
}
//从右边截取$num个字节
function right($str,$num)
{
$value="";
$value=substr(trim($str), -$num);
return $value;
}
//计算字符串长度
function len_var($str)
{
$num=strlen(trim($str));
$value=0;
for($i = 0; $i <$num; $i++) {
if(ord(substr(trim($str), $i, 1)) > 0xa0){
$value += 1;
$i++;
}else{
$value += 1;
}
}
return $value;
}
//计算字符串字节数
function len($str)
{
return strlen(trim($str));
}
//日期时间格式化函数
function formatDateTime($time,$way)
{
//分解时间串,格式为:0000-00-00 00:00:00
$strDateTime = explode(" ",$time);
//分解年月日;
$strDate = explode("-",$strDateTime[0]);
$year = $strDate[0];
$month = $strDate[1];
$day = $strDate[2];
//分解时分秒;
$strTime = explode(":",$strDateTime[1]);
$hour = $strTime[0];
$minute = $strTime[1];
$second = $strTime[2];
switch ($way)
{
case 1://得到年月日;
$strDateTime = $year."-".$month."-".$day;
break;
case 2://月日;
$strDateTime = $month."-".$day;
break;
case 3://得到时分;
$strDateTime = $hour.":".$minute.":".$second;
break;
}
return $strDateTime;
}
//将美国日期时间转换为中国时间,参数$datetime的格式如:2008-09-05 08:04:12
function date_cn($datetime_str,$t=0){
if ( $t == 1 ){ //当$t=1时,$datetime_str的值必须为时间戳格式
$dt = $datetime_str;
}else{ //否则,将其转换为时间戳格式
$dt = strtotime($datetime_str); //将日期时间转换为时间戳格式,如:1220601852
}
if ( trim($dt)<>"" ){
//将美国时间加8个小时,返回值如:2008-09-05 16:04:12
$datetime_str = date("Y-m-d H:i:s", $dt+8*3600);
}
return $datetime_str;
}
//解码JS的escape URL编码
function js_unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++){
if ($str[$i] == '%' && $str[$i+1] == 'u'){
$val = hexdec(substr($str, $i+2, 4));
if ($val < 0x7f) $ret .= chr($val);
else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
$i += 5;
}else if ($str[$i] == '%'){
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
}else $ret .= $str[$i];
}
return $ret;
}
//加密,十六进制
function str2hex($s)
{
$r = "";
$hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
for ($i=0; $i<strlen($s); $i++)
$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);
return $r;
}
//解密,十六进制
function hex2str($s)
{
$r = "";
for ( $i = 0; $i<strlen($s); $i += 2)
{
$x1 = ord($s{$i});
$x1 = ($x1>=48 && $x1<58) ? $x1-48 : $x1-97+10;
$x2 = ord($s{$i+1});
$x2 = ($x2>=48 && $x2<58) ? $x2-48 : $x2-97+10;
$r .= chr((($x1 << 4) & 0xf0) | ($x2 & 0x0f));
}
return $r;
}
//截取指定长度的中英文混合字符串,保证不出现乱码的情况
function my_substr($str,$limit_length) {
$return_str = "";//返回的字符串
$len = mb_strlen($str,'utf8');// 以utf-8格式求字符串的长度,每个汉字算一个长度
if ( $len > $limit_length ){
$omit = "...";
}else{
$limit_length = $len;
}
for ($i = 0; $i < $limit_length; $i++) {
$curr_char = mb_substr($str,$i,1,'utf8');//以utf-8格式取得第$i个位置的字符,取的长度为1
$curr_length = ord($curr_char) > 127 ? 2 : 1;//如果大于127,则此字符为汉字,算两个长度
$return_str .= $curr_char;
}
return $return_str.$omit;
}
?>
//字符串标记转换
function str_format($str)
{
$str = str_replace ("&","&",$str);
$str = str_replace("'",''',$str);
$str = str_replace('"','"',$str);
$str = str_replace('\\','\',$str);
$str = str_replace ("<","<",$str);
$str = str_replace (">",">",$str);
return $str;
}
//特殊符号过滤,主要用于表单提交的数据
function frm_str_format($str)
{
$str = str_replace(chr(92).chr(39),"'",$str); //过滤单引号\'
$str = str_replace(chr(39),"'",$str); //过滤单引号\'
$str = str_replace(chr(92).chr(34),""",$str); //过滤双引号\"
$str = str_replace(chr(34),""",$str); //过滤双引号
$str = str_replace(chr(92).chr(92),"\",$str); //过滤反斜杆\,form表单提交时会将\转换为\\,所以要将其还原为\
$str = str_replace(chr(92),"\",$str); //过滤反斜杆\
return $str;
}
//将GB2312转换为UTF-8编码, 发现有的服务器不支持iconv这个函数
function code_u($str)
{
$str=iconv("GB2312//TRANSLIT","UTF-8",$str);
return $str;
}
//将UTF-8转换为GB2312编码
function code_g($str)
{
$str=iconv("UTF-8","GB2312//TRANSLIT",$str);
return $str;
}
//弹出对话框
function alert($str)
{
echo "<script>alert('$str');</script>";
}
//返回上一步
function goback()
{
echo "<script>history.go(-1);</script>";
}
//页面跳转
function location($url)
{
echo "<script>location.href='$url';</script>";//客户端跳转
//header("location:$url"); //PHP服务器端跳转
}
//无提示关闭窗口
function window_close()
{
echo "<script>window.opener=null;window.close();</script>";
}
//返回日期时间值
function now()
{
return date( "Y-m-d H:i:s");
}
//从左边截取$num个字节
function left($str,$num)
{
$value="";
for($i = 0; $i <$num; $i++){
if(ord(substr(trim($str), $i, 1)) > 0xa0){
if ($i<>$num-1){
$value .= substr(trim($str), $i, 2);
$i++;
}
}else{
$value .= substr(trim($str), $i, 1);
}
}
return $value;
}
//从左边截取$num个字符
function left_var($str,$num)
{
$value="";
for($i = 0; $i <$num; $i++){
if(ord(substr(trim($str), $i, 1)) > 0xa0){
$value .= substr(trim($str), $i, 2);
$i++;
$num++;
}else{
$value .= substr(trim($str), $i, 1);
}
}
return $value;
}
//从右边截取$num个字符
function right_var($str,$num)
{
//本函数还未实现
return $str;
}
//从右边截取$num个字节
function right($str,$num)
{
$value="";
$value=substr(trim($str), -$num);
return $value;
}
//计算字符串长度
function len_var($str)
{
$num=strlen(trim($str));
$value=0;
for($i = 0; $i <$num; $i++) {
if(ord(substr(trim($str), $i, 1)) > 0xa0){
$value += 1;
$i++;
}else{
$value += 1;
}
}
return $value;
}
//计算字符串字节数
function len($str)
{
return strlen(trim($str));
}
//日期时间格式化函数
function formatDateTime($time,$way)
{
//分解时间串,格式为:0000-00-00 00:00:00
$strDateTime = explode(" ",$time);
//分解年月日;
$strDate = explode("-",$strDateTime[0]);
$year = $strDate[0];
$month = $strDate[1];
$day = $strDate[2];
//分解时分秒;
$strTime = explode(":",$strDateTime[1]);
$hour = $strTime[0];
$minute = $strTime[1];
$second = $strTime[2];
switch ($way)
{
case 1://得到年月日;
$strDateTime = $year."-".$month."-".$day;
break;
case 2://月日;
$strDateTime = $month."-".$day;
break;
case 3://得到时分;
$strDateTime = $hour.":".$minute.":".$second;
break;
}
return $strDateTime;
}
//将美国日期时间转换为中国时间,参数$datetime的格式如:2008-09-05 08:04:12
function date_cn($datetime_str,$t=0){
if ( $t == 1 ){ //当$t=1时,$datetime_str的值必须为时间戳格式
$dt = $datetime_str;
}else{ //否则,将其转换为时间戳格式
$dt = strtotime($datetime_str); //将日期时间转换为时间戳格式,如:1220601852
}
if ( trim($dt)<>"" ){
//将美国时间加8个小时,返回值如:2008-09-05 16:04:12
$datetime_str = date("Y-m-d H:i:s", $dt+8*3600);
}
return $datetime_str;
}
//解码JS的escape URL编码
function js_unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++){
if ($str[$i] == '%' && $str[$i+1] == 'u'){
$val = hexdec(substr($str, $i+2, 4));
if ($val < 0x7f) $ret .= chr($val);
else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
$i += 5;
}else if ($str[$i] == '%'){
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
}else $ret .= $str[$i];
}
return $ret;
}
//加密,十六进制
function str2hex($s)
{
$r = "";
$hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
for ($i=0; $i<strlen($s); $i++)
$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);
return $r;
}
//解密,十六进制
function hex2str($s)
{
$r = "";
for ( $i = 0; $i<strlen($s); $i += 2)
{
$x1 = ord($s{$i});
$x1 = ($x1>=48 && $x1<58) ? $x1-48 : $x1-97+10;
$x2 = ord($s{$i+1});
$x2 = ($x2>=48 && $x2<58) ? $x2-48 : $x2-97+10;
$r .= chr((($x1 << 4) & 0xf0) | ($x2 & 0x0f));
}
return $r;
}
//截取指定长度的中英文混合字符串,保证不出现乱码的情况
function my_substr($str,$limit_length) {
$return_str = "";//返回的字符串
$len = mb_strlen($str,'utf8');// 以utf-8格式求字符串的长度,每个汉字算一个长度
if ( $len > $limit_length ){
$omit = "...";
}else{
$limit_length = $len;
}
for ($i = 0; $i < $limit_length; $i++) {
$curr_char = mb_substr($str,$i,1,'utf8');//以utf-8格式取得第$i个位置的字符,取的长度为1
$curr_length = ord($curr_char) > 127 ? 2 : 1;//如果大于127,则此字符为汉字,算两个长度
$return_str .= $curr_char;
}
return $return_str.$omit;
}
?>
文章来源:桂林唯创网络