【发布时间】:2011-06-08 21:29:47
【问题描述】:
我现在有一些关于隐藏某些字符的问题...我想在帐户管理中隐藏用户名的前几个字符。这是我的问题: 在帐户管理中,我在表中有 $username,它取自 DB,我需要这个用户名不要像这样显示: 用户名 => **rname - 只需使用 php 或类似的网页代码将几个首字符替换为“”。
【问题讨论】:
我现在有一些关于隐藏某些字符的问题...我想在帐户管理中隐藏用户名的前几个字符。这是我的问题: 在帐户管理中,我在表中有 $username,它取自 DB,我需要这个用户名不要像这样显示: 用户名 => **rname - 只需使用 php 或类似的网页代码将几个首字符替换为“”。
【问题讨论】:
你可以使用这样的东西
<?php
$username = theusernamehere;
$userhide = str_pad(substr($username, -4), strlen($username), 'x', STR_PAD_LEFT);
$userhide = str_replace('xxxx','xx',$userhide);
echo $userhide;
?>
【讨论】:
我假设您也不想让他们知道用户名中有多少个字符?使用substr_replace()
$val = 'username';
$output = substr_replace($val, '**', 0, -5);
输出:**rname
当然,如果用户名较短,这是行不通的。你可以这样做
$output = substr_replace($val, '**', 0, 3); // or some other length value
【讨论】:
substr($username, 2);
从字符串中删除前两个字符。
【讨论】:
你可以的;
print '***'.substr($username, 3);
【讨论】:
我不太确定你想说什么。你想达到这个目的吗?
echo '***', substr($username, 3);
【讨论】:
echo '***' . substr($username, 3);
【讨论】: