【问题标题】:How to hide text printed from DB如何隐藏从 DB 打印的文本
【发布时间】:2011-06-08 21:29:47
【问题描述】:

我现在有一些关于隐藏某些字符的问题...我想在帐户管理中隐藏用户名的前几个字符。这是我的问题: 在帐户管理中,我在表中有 $username,它取自 DB,我需要这个用户名不要像这样显示: 用户名 => **rname - 只需使用 php 或类似的网页代码将几个首字符替换为“”。

【问题讨论】:

    标签: php text show-hide


    【解决方案1】:

    你可以使用这样的东西

    <?php
    $username = theusernamehere;
    $userhide = str_pad(substr($username, -4), strlen($username), 'x', STR_PAD_LEFT);
    $userhide = str_replace('xxxx','xx',$userhide);
    echo $userhide;
    
    ?>
    

    【讨论】:

    • 这是您问题的答案。你试过了吗?
    【解决方案2】:

    我假设您也不想让他们知道用户名中有多少个字符?使用substr_replace()

    $val = 'username';
    $output = substr_replace($val, '**', 0, -5);
    

    输出:**rname

    当然,如果用户名较短,这是行不通的。你可以这样做

    $output = substr_replace($val, '**', 0, 3); // or some other length value
    

    【讨论】:

    • 是的,第三个参数是替换的开始,第四个是结束。
    【解决方案3】:
    substr($username, 2);
    

    从字符串中删除前两个字符。

    【讨论】:

      【解决方案4】:

      你可以的;

      print '***'.substr($username, 3);
      

      【讨论】:

        【解决方案5】:

        我不太确定你想说什么。你想达到这个目的吗?

        echo '***', substr($username, 3);
        

        【讨论】:

          【解决方案6】:
          echo '***' . substr($username, 3);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-19
            • 1970-01-01
            • 2021-11-04
            • 1970-01-01
            • 2011-09-14
            相关资源
            最近更新 更多