简体   繁体   English

如何用X隐藏名称的字符?

[英]How to hide characters of a name by X?

I want to display only first three and last three characters of a name. 我只想显示名称的前三个和后三个字符。 Other characters should be hidden by X. 其他字符应由X隐藏。
For example, 例如,

$name = "ELVIS MASTAINE KANGABAM";  

I want to echo the above name like the following: 我想像上面这样呼应上面的名字:

$hidden_name = "ELVXX XXXXXXXX XXXXXBAM";  

echo $hidden_name;  

Please help! 请帮忙! Thank you! 谢谢!

You could use substr and concatenation eg: 您可以使用substr和串联,例如:

$hidden_name = substr($name ,0, 3). str_repeat("x", strlen($name)-6) . substr($name , -3);
echo $hidden_name;  

I use preg_replace on all aZ chars from substr 3 -> -3 and replace them with X. 我在substr 3-> -3的所有aZ字符上使用preg_replace并将其替换为X。

$name = "ELVIS MASTAINE KANGABAM";  
$hidden_name = substr($name, 0,3) . preg_replace("/([a-zA-Z])/", "x", substr($name,3,-3)) . substr($name, -3); 
echo $hidden_name;

https://3v4l.org/CEK9B https://3v4l.org/CEK9B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM