Mercurial > hg > rc1
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 6:cec75ba50afc | 7:430dbd5346f7 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Sabre\VObject; | |
| 4 | |
| 5 /** | |
| 6 * Useful utilities for working with various strings. | |
| 7 * | |
| 8 * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/). | |
| 9 * @author Evert Pot (http://evertpot.com/) | |
| 10 * @license http://sabre.io/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 public function isUTF8($str) { | |
| 21 | |
| 22 // Control characters | |
| 23 if (preg_match('%[\x00-\x08\x0B-\x0C\x0E\x0F]%', $str)) { | |
| 24 return false; | |
| 25 } | |
| 26 | |
| 27 return (bool)preg_match('%%u', $str); | |
| 28 | |
| 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 * @return string | |
| 39 */ | |
| 40 static public function convertToUTF8($str) { | |
| 41 | |
| 42 $encoding = mb_detect_encoding($str , array('UTF-8','ISO-8859-1', 'WINDOWS-1252'), true); | |
| 43 | |
| 44 switch($encoding) { | |
| 45 case 'ISO-8859-1' : | |
| 46 $newStr = utf8_encode($str); | |
| 47 break; | |
| 48 /* Unreachable code. Not sure yet how we can improve this | |
| 49 * situation. | |
| 50 case 'WINDOWS-1252' : | |
| 51 $newStr = iconv('cp1252', 'UTF-8', $str); | |
| 52 break; | |
| 53 */ | |
| 54 default : | |
| 55 $newStr = $str; | |
| 56 | |
| 57 } | |
| 58 | |
| 59 // Removing any control characters | |
| 60 return (preg_replace('%(?:[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%', '', $newStr)); | |
| 61 | |
| 62 } | |
| 63 | |
| 64 } | |
| 65 |
