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