1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function half_replace($str){//定义一个字符串 $len = strlen($str)/2;//获取字符串长度 return substr_replace($str,str_repeat('*',$len),floor(($len)/2),$len); } /* //*的个数,like:str_repeat('*',4)---****. str_repeat('*',$len) //原字符串,要插入的字符串,开始位置,规定要替换多少个字符,与str_repeat数目保持相同 substr_replace(string,replacement,start,length) //函数向下舍入为最接近的整数 like:4.5--4 5.8--5 floor() */ 使用方法: PHP substr_replace() 函数 PHP str_repeat() 函数 PHP floor() 函数 |