Encoding::toAscii()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.4285
cc 2
eloc 12
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Lbc\Helper;
4
5
class Encoding
6
{
7
    /**
8
     * Replace accent and remove unknown chars
9
     *
10
     * @param string $string
11
     * @return string
12
     */
13 10
    public static function toAscii($string)
14
    {
15 10
        $alnumPattern = '/^[a-zA-Z0-9 ]+$/';
16
17 10
        $string = iconv(
18 10
            mb_detect_encoding($string),
19 10
            'ASCII//TRANSLIT',
20 10
            $string
21
        );
22
23 10
        $ret = array_map(function ($chr) use ($alnumPattern) {
24 10
            if (preg_match($alnumPattern, $chr)) {
25 10
                return $chr;
26
            }
27 10
            return '';
28 10
        }, str_split($string));
29
30 10
        return implode($ret);
31
    }
32
}
33