初学php时就知道利用替换函数来替换指定的数字,但要替换指定位置我们需要简单处理一下才行,下面我介绍几个例子把指定位星号显示出来。
例子,用户名
代码如下 复制代码
function cut_str($string, $sublen, $start = 0, $code = 'utf-8')
{
if($code == 'utf-8')
{
$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]/;
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen));
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';www.111cn.net
for($i=0; $i {
if($i>=$start && $i {
if(ord(substr($string, $i, 1))>129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
//if(strlen($tmpstr) return $tmpstr;
}
}
示例
代码如下 复制代码
$str = 如来神掌;
echo cut_str($str, 1, 0).'**'.cut_str($str, 1, -1);
//输出:如**掌
下面附两个ip地址与手机号星号显示
初学php,看到别人是通过合并和拆分数组进行这个操作!所以转过来!!
代码如下 复制代码
手机号的
代码如下 复制代码