2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHPで名前のマスキングをする

Posted at

環境

  • php:8.1

対象

  • 名前のマスキングをしたい人

対応

今回は以下のような関数を作りました

Controller.php
    private function nameMasking(string $value)
    {
        // 正規表現を使用して文字列から空白(半角スペースと全角スペース)を取り除く(名前と苗字にスペースがあると綺麗にマスキングされないため)
        $stringValue  = preg_replace("/( | )/", "", $value);
        // 文字列を配列化
        $arr = mb_str_split($stringValue);
        $itemCount = 1;
        $replaceValue = '';
        foreach ($arr as $item) {
            if ($itemCount % 2 === 0) {
                // 偶数文字目だったら●に置き換え
                $replaceValue = $replaceValue . '●';
            } else {
                // 奇数文字目だったらそのまま
                $replaceValue = $replaceValue . $item;
            }
            $itemCount++;
        }

        return $replaceValue;
    }
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?