Completed
Pull Request — master (#40)
by Michael
03:26 queued 01:00
created

StringUtil::stripAnsiEscapeSequence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PhpSchool\CliMenu\Util;
4
5
/**
6
 * Class StringUtil
7
 *
8
 * @package PhpSchool\CliMenu\Util
9
 * @author Michael Woodward <[email protected]>
10
 */
11
class StringUtil
12
{
13
    /**
14
     * Minimal multi-byte wordwrap implementation
15
     * which also takes break length into consideration
16
     *
17
     * @param string $str
18
     * @param int $width
19
     * @param string $break
20
     * @return string
21
     */
22
    public static function wordwrap($str, $width, $break = "\n")
23
    {
24
        $length = 0;
25
        return implode(" ", array_map(function ($word) use (&$length, $width, $break) {
26
            $length += (mb_strlen($word) + 1);
27
28
            if ($length > $width) {
29
                $length = mb_strlen($break);
30
                return sprintf("%s%s", $break, $word);
31
            }
32
33
            return $word;
34
        }, explode(" ", $str)));
35
    }
36
37
    /**
38
     * @param string $str
39
     * @return string
40
     */
41
    public static function stripAnsiEscapeSequence($str)
42
    {
43
        return preg_replace('/\x1b[^m]*m/', '', $str);
44
    }
45
}
46