php obscure string

Solutions on MaxInterview for php obscure string by the best coders in the world

showing results for - "php obscure string"
Emil
08 Aug 2020
1/**
2 * @param string|string[] $plain
3 * @param int             $revealStart
4 * @param int             $revealEnd
5 * @param string          $obscuration
6 * @return string|string[]
7 */
8function obscure(
9    $plain,
10    int $revealStart = 1,
11    int $revealEnd = 0,
12    string $obscuration = '*'
13) {
14    if (is_array($plain)) {
15        return array_map(
16            function ($plainPart) use ($revealStart, $revealEnd, $obscuration) {
17                return obscure($plainPart, $revealStart, $revealEnd, $obscuration);
18            },
19            $plain
20        );
21    }
22    $plain = (string) $plain;
23    return mb_substr($plain, 0, $revealStart)
24        . str_repeat(
25            $obscuration,
26            max(
27                0,
28                mb_strlen($plain) -
29                ($revealStart + $revealEnd)
30            )
31        )
32        . mb_substr(
33            $plain,
34            -$revealEnd,
35            $revealEnd
36        );
37}