CoreRuntime::protectMailAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\Twig\Runtime;
15
16
use Twig\Extension\RuntimeExtensionInterface;
17
18
class CoreRuntime implements RuntimeExtensionInterface
19
{
20
    /**
21
     * Protect a given mail address by finding the text 'x@y' and replacing
22
     * it with HTML entities. This provides protection against email harvesters.
23
     */
24
    public function protectMailAddress(string $string): string
25
    {
26
        $string = preg_replace_callback(
27
            '/(.)@(.)/s',
28
            static function ($m) {
29
                return '&#' . sprintf('%03d', ord($m[1])) . ';&#064;&#' . sprintf('%03d', ord($m[2])) . ';';
30
            },
31
            $string
32
        );
33
34
        return $string;
35
    }
36
}
37