JapanStringUtil::standardize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of JapanUtil.
5
 *
6
 * (c) Takashi OGAWA
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace NinjaAnija\JapanUtil;
13
14
/**
15
 * 日本語文字列を扱うためのユーティリティ.
16
 *
17
 * @author Takashi OGAWA
18
 *
19
 */
20
class JapanStringUtil
21
{
22
23
    /**
24
     * 日本語文字列を下記のように標準化します
25
     *
26
     *  - 改行不可スペース(C2A0)を半角スペース変換
27
     *  - mb_convert_kana KVas
28
     *    全角ハイフンは半角ハイフンに変換(電話番号や郵便番号で利用を想定)
29
     *    全角ダッシュはそのままなので注意
30
     *    その他注意は右記リンクを参考に: http://qiita.com/hrdaya/items/470b338e7c0014fe6dc7
31
     *  - 複数連続した半角スペースをひとつに変換
32
     *  - trim
33
     *
34
     * @param  string  $str
35
     * @return string
36
     */
37 9
    public static function standardize($str)
38
    {
39 9
        $test = str_replace("\xc2\xa0", ' ', $str);
40 9
        return trim(preg_replace('/\s{2,}/', ' ', mb_convert_kana($test, 'KVas')));
41
    }
42
}
43