首页 > 其他 > 详细

部分字符串替换为*

时间:2016-07-04 18:50:02      阅读:91      评论:0      收藏:0      [点我收藏+]
 1 function cut_str($string, $sublen, $start = 0, $code = ‘UTF-8‘)
 2 {
 3     if($code == ‘UTF-8‘)
 4     {
 5         $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
 6         preg_match_all($pa, $string, $t_string);
 7 
 8         if(count($t_string[0]) - $start > $sublen) return join(‘‘, array_slice($t_string[0], $start, $sublen));
 9         return join(‘‘, array_slice($t_string[0], $start, $sublen));
10     }
11     else
12     {
13         $start = $start*2;
14         $sublen = $sublen*2;
15         $strlen = strlen($string);
16         $tmpstr = ‘‘;
17 
18         for($i=0; $i< $strlen; $i++)
19         {
20             if($i>=$start && $i< ($start+$sublen))
21             {
22                 if(ord(substr($string, $i, 1))>129)
23                 {
24                     $tmpstr.= substr($string, $i, 2);
25                 }
26                 else
27                 {
28                     $tmpstr.= substr($string, $i, 1);
29                 }
30             }
31             if(ord(substr($string, $i, 1))>129) $i++;
32         }
33         //if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
34         return $tmpstr;
35     }
36 }

字符串部分用*代替,

调用为 

echo cut_str($str, 1, 0).‘**‘.cut_str($str, 1, -1);

  

部分字符串替换为*

原文:http://www.cnblogs.com/ningsir/p/5641145.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!