annotate plugins/libcalendaring/lib/Sabre/VObject/StringUtil.php @ 4:888e774ee983

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