以下是不同实现方法:
使用str_pad()函数:
$name = “张三”;
s t a r r e d N a m e = s t r p a d ( s u b s t r ( starredName = str_pad(substr( starredName=strpad(substr(name, 0, 1), mb_strlen($name), “*”, STR_PAD_RIGHT);
echo $starredName;
使用str_repeat()函数:
$name = “张三”;
s t a r r e d N a m e = s t r r e p e a t ( " ∗ " , m b s t r l e n ( starredName = str_repeat("*", mb_strlen( starredName=strrepeat("∗",mbstrlen(name) - 1) . mb_substr($name, -1);
echo $starredName;
使用正则表达式替换:
$name = “张三”;
$starredName = preg_replace(“/.(?=.?)/u", "”, $name);
echo $starredName;
使用循环遍历替换:
$name = “张三”;
s t a r r e d N a m e = " " ; f o r ( starredName = ""; for ( starredName="";for(i = 0; i < m b s t r l e n ( i < mb_strlen( i<mbstrlen(name) - 1; $i++) {
$starredName .= “*”;
}
s t a r r e d N a m e . = m b s u b s t r ( starredName .= mb_substr( starredName.=mbsubstr(name, -1);
echo $starredName;