php(二)

来源:互联网 发布:防9158聊天室源码 编辑:程序博客网 时间:2024/05/21 15:46


084 
085/**
086 * 字符串截取,支持中文和其他编码
087 * @param  [string]  $str     [字符串]
088 * @param  integer $start   [起始位置]
089 * @param  integer $length  [截取长度]
090 * @param  string  $charset [字符串编码]
091 * @param  boolean $suffix  [是否有省略号]
092 * @return [type]           [description]
093 */
094function msubstr($str$start=0, $length=15, $charset="utf-8"$suffix=true) {
095    if(function_exists("mb_substr")) {
096        return mb_substr($str$start$length$charset);
097    elseif(function_exists('iconv_substr')) {
098        return iconv_substr($str,$start,$length,$charset);
099    }
100    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
101    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
102    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
103    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
104    preg_match_all($re[$charset], $str$match);
105    $slice = join("",array_slice($match[0], $start$length));
106    if($suffix) {
107        return $slice."…";
108    }
109    return $slice;
110}
111 
112/**
113 * php 实现js escape 函数
114 * @param  [type] $string   [description]
115 * @param  string $encoding [description]
116 * @return [type]           [description]
117 */
118function escape($string$encoding 'UTF-8'){
119  $return = null;
120  for ($x = 0; $x < mb_strlen($string$encoding);$x ++)
121  {
122    $str = mb_substr($string$x, 1, $encoding);
123    if (strlen($str) > 1) { // 多字节字符
124      $return .= "%u" strtoupper(bin2hex(mb_convert_encoding($str'UCS-2',$encoding)));
125    else {
126      $return .= "%" strtoupper(bin2hex($str));
127    }
128  }
129  return $return;
130}
131/**
132 * php 实现 js unescape函数
133 * @param  [type] $str [description]
134 * @return [type]      [description]
135 */
136function unescape($str) {
137    $str = rawurldecode($str);
138    preg_match_all("/(?:%u.{4})|.{4};|&#\d+;|.+/U",$str,$r);
139    $ar $r[0];
140    foreach($ar as $k=>$v) {
141        if(substr($v,0,2) == "%u"){
142            $ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
143        elseif(substr($v,0,3) == "") {
144            $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
145        elseif(substr($v,0,2) == "&#") {
146            echo substr($v,2,-1)."";
147            $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
148        }
149    }
150    return join("",$ar);
151}
152 
153/**
154 * 数字转人名币
155 * @param  [type] $num [description]
156 * @return [type]      [description]
157 */
158function num2rmb ($num) {
159    $c1 "零壹贰叁肆伍陆柒捌玖";
160    $c2 "分角元拾佰仟万拾佰仟亿";
161    $num round($num, 2);
162    $num $num * 100;
163    if (strlen($num) > 10) {
164        return "oh,sorry,the number is too long!";
165    }
166    $i = 0;
167    $c "";
168    while (1) {
169        if ($i == 0) {
170            $n substr($numstrlen($num)-1, 1);
171        else {
172            $n $num % 10;
173        }
174        $p1 substr($c1, 3 * $n, 3);
175        $p2 substr($c2, 3 * $i, 3);
176        if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
177            $c $p1 $p2 $c;
178        else {
179            $c $p1 $c;
180        }
181        $i $i + 1;
182        $num $num / 10;
183        $num = (int)$num;
184        if ($num == 0) {
185            break;
186        }
187    }
188    $j = 0;
189    $slen strlen($c);
190    while ($j $slen) {
191        $m substr($c$j, 6);
192        if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
193            $left substr($c, 0, $j);
194            $right substr($c$j + 3);
195            $c $left $right;
196            $j $j-3;
197            $slen $slen-3;
198        }
199        $j $j + 3;
200    }
201    if (substr($cstrlen($c)-3, 3) == '零') {
202        $c substr($c, 0, strlen($c)-3);
203    // if there is a '0' on the end , chop it out
204    return $c "整";
205}
206 
207 
原创粉丝点击