标签:value strlen amp bool length charset reg 中文 rcu
/** * 格式化金额为整数或两位小数 * @param float $money 金额 * @param bool $flag true 四舍五入,false向下取2位小数,小数点第三3位后舍去 * @return string 整数或两位小数 */ function present($money, $flag = true) { $money = $flag ? sprintf("%.2f", $money) : (floor(bcmul($money, 100))/100); return number_format($money, 2, ‘.‘, ‘‘); }
/** * 过滤客户端提交的数据 * 转义字符或数组为HTML,htmlspecialchars 替代函数,转义<>"‘,支持数组 * @param string or array $array:要转义的字符或数组 * @param string $quote:ENT_COMPAT:转换双引号。ENT_QUOTES(默认):单双引号都转义,ENT_NOQUOTES:不转义单双引号 */ function htmlChars(&$array, $quote=ENT_QUOTES) { if (is_array($array)) { foreach ($array as $key => $value) { $array[$key] = htmlChars($value, $quote); } } else { $array = htmlspecialchars($array, $quote); } return $array; }
/** * 截取字符串 * @param string $string:要截取的字符串 * @param int $length:截取的宽度,不是字节数 * @param string $dot:截取的后缀 * @param string $charset:字符编码 * @return string */ function cutStr($string, $length, $dot=‘...‘, $charset=‘utf-8‘) { if (strlen($string) <= $length) { return $string; } $str = str_replace(array(‘&‘,‘"‘,‘<‘,‘>‘,‘ ‘), array(‘&‘,‘"‘,‘<‘,‘>‘,‘ ‘), $string); $strcut = ‘‘; if (strtolower($charset) == ‘utf-8‘) { $n = $tn = $noc = 0; while ($n < strlen($str)) { $t = ord($str[$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($str, 0, $n); } else { for ($i = 0; $i < $length; $i++) { $strcut .= ord($str[$i]) > 127 ? $str[$i].$str[++$i] : $str[$i]; } } $strcut = str_replace(array(‘"‘,‘<‘,‘>‘), array(‘"‘,‘<‘,‘>‘), $strcut); $strcut != $string && $strcut .= $dot; return $strcut; }
/** * 截取中文字符串,支持gb2312,gbk,utf-8,big5 * @param string $str 要截取的字串 * @param int $start 截取起始位置 * @param int $length 截取长度 * @param string $charset utf-8|gb2312|gbk|big5 编码 * @param $suffix 是否加尾缀 */ function chCutStr($str, $start = 0, $length, $charset = "utf-8", $suffix = ‘...‘) { if (function_exists(‘mb_substr‘)) { if (mb_strlen($str, $charset) <= $length) return $str; $slice = mb_substr($str, $start, $length, $charset); } else { $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); if (count($match[0]) <= $length) return $str; $slice = join("", array_slice($match[0], $start, $length)); } if ($suffix == false) return $slice; return $slice . $suffix; }
/** * 对象转数组 * @param $array * @return */ function objectToArray($array) { if (is_object($array)) { $array = (array)$array; } if (is_array($array)) { foreach ($array as $key => $value) { $array[$key] = objectToArray($value); } } return $array; }
标签:value strlen amp bool length charset reg 中文 rcu
原文地址:https://www.cnblogs.com/sai564/p/12609080.html