annotate vendor/sabre/vobject/lib/StringUtil.php @ 7:430dbd5346f7

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