PHP-检查字符串的中间


PHP - Censor the middle of a string

我不希望使用PHP脚本,一个字符串的中间部分会被审查。

例如:developer@example.com,将变成:develo*********le.com.

什么是一种干净有效的方法来做到这一点?

我现在的代码:

                    $target = "example@example.com";
                    $count = strlen($target) - 7;
                    $asterix = '';
                    for ($a = 0; $a <= $count; $a++) {
                        $asterix .= '*';
                    }
                    $output = substr($target, 0, 4) . $asterix . substr($target, -3);
                    echo $output;

编辑

你们所有人都在对其中的跨骑(**)进行硬编码……我希望它是准确的。

解决了bij Sadiq。你才是真正的MVP。

您可以执行类似的操作

$target = "example@example.com";
$count = strlen($target) - 7;
$output = substr_replace($target, str_repeat('*', $count), 4, $count);
echo $output;

它利用了CCD_ 1和CCD_。

使用PHP的substr函数。

http://php.net/manual/en/function.substr.php

$emailAddr = "developer@mydomain.com";
$emailAddrHidden = substr($emailAddr, 0, 6) . "*******" . substr($emailAddr, -6, 6);
echo $emailAddrHidden;

当电子邮件地址的长度发生变化时,此代码将起作用:

$email = 'developer@example.com';
$length = strlen($email); //Find the string length
$astr='';
for ($i=1;$i<=$length-12;$i++){$astr .= '*';}
echo substr($email, 0, 6) . $astr . substr($email, -6 , 6);