1<?php
2
3namespace Sabre\VObject;
4
5/**
6 * Useful utilities for working with various strings.
7 *
8 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
9 * @author Evert Pot (http://evertpot.com/)
10 * @license http://sabre.io/license/ Modified BSD License
11 */
12class StringUtil {
13
14    /**
15     * Returns true or false depending on if a string is valid UTF-8.
16     *
17     * @param string $str
18     *
19     * @return bool
20     */
21    static function isUTF8($str) {
22
23        // Control characters
24        if (preg_match('%[\x00-\x08\x0B-\x0C\x0E\x0F]%', $str)) {
25            return false;
26        }
27
28        return (bool)preg_match('%%u', $str);
29
30    }
31
32    /**
33     * This method tries its best to convert the input string to UTF-8.
34     *
35     * Currently only ISO-5991-1 input and UTF-8 input is supported, but this
36     * may be expanded upon if we receive other examples.
37     *
38     * @param string $str
39     *
40     * @return string
41     */
42    static function convertToUTF8($str) {
43
44        $encoding = mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1', 'WINDOWS-1252'], true);
45
46        switch ($encoding) {
47            case 'ISO-8859-1' :
48                $newStr = utf8_encode($str);
49                break;
50            /* Unreachable code. Not sure yet how we can improve this
51             * situation.
52            case 'WINDOWS-1252' :
53                $newStr = iconv('cp1252', 'UTF-8', $str);
54                break;
55             */
56            default :
57                 $newStr = $str;
58
59        }
60
61        // Removing any control characters
62        return (preg_replace('%(?:[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%', '', $newStr));
63
64    }
65
66}
67