简体   繁体   English

如何用x替换字符串的最后15位数字?

[英]How to replace last 15 digits of a string with x?

Let's say I've got a string which I want to display on my user's settings page. 假设我有一个要显示在用户设置页面上的字符串。 It's the HWID of the specific user but I want to hide like the last 15 characters from the string with x for example. 这是特定用户的HWID,但我想隐藏像与字符串中的最后15个字符x的例子。 How to do this ? 这个怎么做 ? Probably preg_replace() .. please let me know. 大概preg_replace() ..请让我知道。

Example string: aaaec315bbfb9184fd9d7c8e165b297d but it will display like aaaec315bbfb9184f************** for exmaple 示例字符串: aaaec315bbfb9184fd9d7c8e165b297d但示例显示为aaaec315bbfb9184f**************

I believe it can only contain an alphanumeric characters since it's a HWID. 我相信它只能包含字母数字字符,因为它是HWID。

$string = substr($string,0,-15)."xxxxxxxxxxxxxxx";

While @hellocde's example does do this, I would just use the native substr_replace function instead as this is what it's intended for. 虽然@hellocde的示例做到了这一点,但我只是使用了本机的substr_replace函数,因为这正是它的目的。

substr_replace($string, 'xxxxxxxxxxxxxx', -15);

Which will produce: 会产生:

aaaec315bbfb9184fdxxxxxxxxxxxxxx

Functional example 功能实例

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

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