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    public 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     * This method tries its best to convert the input string to UTF-8.
33     *
34     * Currently only ISO-5991-1 input and UTF-8 input is supported, but this
35     * may be expanded upon if we receive other examples.
36     *
37     * @param string $str
38     *
39     * @return string
40     */
41    public static function convertToUTF8($str)
42    {
43        $encoding = mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1', 'WINDOWS-1252'], true);
44
45        switch ($encoding) {
46            case 'ISO-8859-1':
47                $newStr = utf8_encode($str);
48                break;
49            /* Unreachable code. Not sure yet how we can improve this
50             * situation.
51            case 'WINDOWS-1252' :
52                $newStr = iconv('cp1252', 'UTF-8', $str);
53                break;
54             */
55            default:
56                 $newStr = $str;
57        }
58
59        // Removing any control characters
60        return preg_replace('%(?:[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%', '', $newStr);
61    }
62}
63