[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Copyright 2014 Horde LLC (http://www.horde.org/) 4 * 5 * See the enclosed file COPYING for license information (LGPL). If you 6 * did not receive this file, see http://www.horde.org/licenses/lgpl21. 7 * 8 * @category Horde 9 * @copyright 2014 Horde LLC 10 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 11 * @package Util 12 */ 13 14 /** 15 * Provides utility methods used to transliterate a string. 16 * 17 * @author Michael Slusarz <slusarz@horde.org> 18 * @author Jan Schneider <jan@horde.org> 19 * @category Horde 20 * @copyright 2014 Horde LLC 21 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 22 * @package Util 23 * @since 2.4.0 24 */ 25 class Horde_String_Transliterate 26 { 27 /** 28 * Transliterate mapping cache. 29 * 30 * @var array 31 */ 32 static protected $_map; 33 34 /** 35 * Transliterator instance. 36 * 37 * @var Transliterator 38 */ 39 static protected $_transliterator; 40 41 /** 42 * Transliterates an UTF-8 string to ASCII, replacing non-English 43 * characters to their English equivalents. 44 * 45 * Note: there is no guarantee that the output string will be ASCII-only, 46 * since any non-ASCII character not in the transliteration list will 47 * be ignored. 48 * 49 * @param string $str Input string (UTF-8). 50 * 51 * @return string Transliterated string (UTF-8). 52 */ 53 static public function toAscii($str) 54 { 55 switch (true) { 56 case class_exists('Transliterator'): 57 return self::_intlToAscii($str); 58 case extension_loaded('iconv'): 59 return self::_iconvToAscii($str); 60 default: 61 return self::_fallbackToAscii($str); 62 } 63 } 64 65 /** 66 */ 67 static protected function _intlToAscii($str) 68 { 69 if (!isset(self::$_transliterator)) { 70 self::$_transliterator = Transliterator::create( 71 'Any-Latin; Latin-ASCII' 72 ); 73 } 74 return self::$_transliterator->transliterate($str); 75 } 76 77 /** 78 */ 79 static protected function _iconvToAscii($str) 80 { 81 return iconv('UTF-8', 'ASCII//TRANSLIT', $str); 82 } 83 84 /** 85 */ 86 static protected function _fallbackToAscii($str) 87 { 88 if (!isset(self::$_map)) { 89 self::$_map = array( 90 'À' => 'A', 91 'Á' => 'A', 92 'Â' => 'A', 93 'Ã' => 'A', 94 'Ä' => 'A', 95 'Å' => 'A', 96 'Æ' => 'AE', 97 'à' => 'a', 98 'á' => 'a', 99 'â' => 'a', 100 'ã' => 'a', 101 'ä' => 'a', 102 'å' => 'a', 103 'æ' => 'ae', 104 'Þ' => 'TH', 105 'þ' => 'th', 106 'Ç' => 'C', 107 'ç' => 'c', 108 'Ð' => 'D', 109 'ð' => 'd', 110 'È' => 'E', 111 'É' => 'E', 112 'Ê' => 'E', 113 'Ë' => 'E', 114 'è' => 'e', 115 'é' => 'e', 116 'ê' => 'e', 117 'ë' => 'e', 118 'ƒ' => 'f', 119 'Ì' => 'I', 120 'Í' => 'I', 121 'Î' => 'I', 122 'Ï' => 'I', 123 'ì' => 'i', 124 'í' => 'i', 125 'î' => 'i', 126 'ï' => 'i', 127 'Ñ' => 'N', 128 'ñ' => 'n', 129 'Ò' => 'O', 130 'Ó' => 'O', 131 'Ô' => 'O', 132 'Õ' => 'O', 133 'Ö' => 'O', 134 'Ø' => 'O', 135 'ò' => 'o', 136 'ó' => 'o', 137 'ô' => 'o', 138 'õ' => 'o', 139 'ö' => 'o', 140 'ø' => 'o', 141 'Š' => 'S', 142 'ẞ' => 'SS', 143 'ß' => 'ss', 144 'š' => 's', 145 'ś' => 's', 146 'Ù' => 'U', 147 'Ú' => 'U', 148 'Û' => 'U', 149 'Ü' => 'U', 150 'ù' => 'u', 151 'ú' => 'u', 152 'û' => 'u', 153 'Ý' => 'Y', 154 'ý' => 'y', 155 'ÿ' => 'y', 156 'Ž' => 'Z', 157 'ž' => 'z' 158 ); 159 } 160 161 return strtr($str, self::$_map); 162 } 163 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |