如何将密码字符串转换为指定的符号,例如*
或其他符号。
我目前正在处理修改密码页面。
我希望在页面上显示密码,并避免被他人发现,因此我希望将密码字符串转换为与<input type="password" />
相同长度的符号,例如*
。
下面是一个示例:
如何将密码字符串转换为指定的符号,例如*
或其他符号。
我目前正在处理修改密码页面。
我希望在页面上显示密码,并避免被他人发现,因此我希望将密码字符串转换为与<input type="password" />
相同长度的符号,例如*
。
下面是一个示例:
$output_password = str_repeat ('*', strlen ($input_password));
在HTML中不要输出密码,而是创建一个星号(*
)代表密码的表示形式。这可以通过str_repeat函数轻松完成:
<?php
$password = "MyPasswordThatIWantToMask";
$maskedPassword = str_repeat("*", strlen($password));
?>
现在你只需将$maskedPassword
输出为密码字段的值。
然而,另一个非常有趣的问题是:你如何知道用户的密码长度?我真诚地希望你对密码进行哈希处理,而不是以明文形式存储。
$modified = str_repeat( "*", strlen( "secretpass" ) );
echo $modified; // **********
像这样做:
$passwordstring = "password";
$outputpassword = "";
while(strlen($outputpassword)<strlen($passwordstring))
{
$outputpassword .= "*";
}
echo $outputpassword
虽然这里有很多回答展示了str_repeat()
的用法,但我有点怀疑这是否是你想要的。毕竟,任何傻瓜都可以用一个字符填充字符串,在这种情况下这样做没有什么意义,因为我们可以像你正确指出的那样简单地使用<input type="password" />
(是的,你仍然可以从源代码中获取密码,但那么为什么要填充一个混淆字段呢?或者不只是用一个固定数量的*字符填充它?)。
我猜你更像是在寻找这样的东西:
<?php
$thePassword = "thisIsMyPassword";
?>
<input type="text" id="input_user_types_in" value="<?php echo str_repeat('*', strlen($thePassword)); ?>" />
<input type="hidden" id="value_to_post_back" name="value_to_post_back" value="<?php echo $thePassword; ?>" />
<script type="text/javascript">
var textInput = document.getElementById('input_user_types_in');
var hiddenInput = document.getElementById('value_to_post_back');
textInput.onfocus = function() {
this.value = hiddenInput.value;
};
textInput.onblur = function() {
var i;
hiddenInput.value = this.value;
this.value = '';
for (i = 0; i < hiddenInput.value.length; i++) {
this.value = this.value + '*';
}
};
</script>
拿它来fiddle一下吧 ;-)
<input type='password' disabled='disabled' value='somepassword' />