File: /web/data/www.tbbprovision.com/lib/class/str.class.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/3/10
* Time: 23:46
*/
class str
{
function cut_str($str, $lenth, $start = 0,$ext='..')
{ //剪切字符串
$str_0 = array('&', '"', '<', '>', '“', '”');
$str_1 = array('&', '"', '<', '>', '“', '”');
$str = str_replace($str_0, $str_1, $str);
$len = strlen($str);
if ($len <= $lenth) {
return str_replace($str_1, $str_0, $str);
}
$substr = '';
$n = $m = 0;
for ($i = 0; $i < $len; $i++) {
$x = substr($str, $i, 1);
$a = base_convert(ord($x), 10, 2);
$a = substr('00000000' . $a, -8);
if ($n < $start) {
if (substr($a, 0, 3) == 110) {
$i += 1;
} elseif (substr($a, 0, 4) == 1110) {
$i += 2;
}
$n++;
} else {
if (substr($a, 0, 1) == 0) {
$substr .= substr($str, $i, 1);
} elseif (substr($a, 0, 3) == 110) {
$substr .= substr($str, $i, 2);
$i += 1;
} elseif (substr($a, 0, 4) == 1110) {
$substr .= substr($str, $i, 3);
$i += 2;
} else {
$substr .= '';
}
if (++$m >= $lenth) {
break;
}
}
}
return str_replace($str_1, $str_0, $substr).$ext;
}
/**
* json 格式化对象
*/
public static function json_data($row, $fun = 'json_encode')
{
if (!$row) return false;
if (!is_array($row)) {
$row = (array)$row;
}
if ($fun == 'json_encode') {
return json_encode($row, JSON_UNESCAPED_UNICODE);
} else {
return json_decode($row, true);
}
}
/**
* 文本编码
*/
public static function str_code($data, $fun = 'htmlspecialchars')
{ //文本编码
if (!is_array($data)) {
return $fun($data);
}
$new_data = array();
foreach ((array)$data as $k => $v) {
if (is_array($v)) {
$new_data[$k] = self::str_code($v, $fun);
} else {
$new_data[$k] = $fun($data[$k]);
}
}
return $new_data;
}
/**
*
*/
public static function iconver($data, $source = 'UTF-8', $target = 'GBK')
{
global $c;
$chs = new iconver();
if (!is_array($data)) {
return $chs->Convert($data, $source, $target);
}
$new_data = array();
foreach ((array)$data as $k => $v) {
if (is_array($v)) {
$new_data[$k] = self::iconver($v, $source, $target);
} else {
$new_data[$k] = $chs->Convert($data[$k], $source, $target);
}
}
return $new_data;
}
/**
* 随机命名
*/
public static function rand_code($length = 10)
{ //随机命名
global $c;
return substr(md5(time() . mt_rand(100000, 9999999)), 5, 6) . date('Ymdhis');
}
/*
* 格式化输出文本
*/
public static function format($str)
{ //格式化文本
$str = htmlspecialchars($str);
$str = str_replace(' ', ' ', $str);
$str = nl2br($str);
return $str;
}
/*
* 随机生成不指定长度的字符串.
*/
public static function rand_str($length = 8)
{
$result = '';
$str = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789";
for ($i = 0; $i < $length; $i++) {
$start = @mt_rand(0, 61);
$result .= substr($str, $start, 1);
}
return $result;
}
/**
* 获取中文字符的首字母
* @param $s0
* @return null|string
*/
public static function getfirstchar($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 null;
}
/**
* action 入口调用
* 字符串参数变成数组形式
* @param string $string
* @return array|bool
*/
public static function strtoparames($string='')
{
if(!$string){return false;}
$list = @explode('&',trim($string,'&'));
$result = array();
foreach((array)$list as $k=>$v){
$tmp = @explode('=',$v);
$result[$tmp[0]]=$tmp[1];
}
return $result;
}
/**
* 将数组列表中的指定字段转换为日期
* @param array $row
* @param array $field
* @param string $format
* @return array
*/
public static function time_2_date($row=array(),$field=array(),$format='Y/m/d H:i:s')
{
$field = (array)$field;
foreach((array)$row as $k=>$v){
foreach($field as $k2=>$v2){
if(isset($v[$v2])){
$row[$k][$v2] = date($format,$v[$v2]);
}
}
}
return $row;
}
/**
* 将数组列表中的指定字段转换为ip以及地址
* @param array $row
* @param array $field
* @return array
*/
public static function array_ip2str($row=array(),$field=array())
{
$field = (array)$field;
foreach((array)$row as $k=>$v){
foreach($field as $k2=>$v2){
if(isset($v[$v2])){
$row[$k][$v2] = $v[$v2].' ['.f::ip($v[$v2]).']';
}
}
}
return $row;
}
}