[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Tools to help with ASCII in UTF-8 4 * 5 * @package utf8 6 */ 7 8 //-------------------------------------------------------------------- 9 /** 10 * Tests whether a string contains only 7bit ASCII bytes. 11 * You might use this to conditionally check whether a string 12 * needs handling as UTF-8 or not, potentially offering performance 13 * benefits by using the native PHP equivalent if it's just ASCII e.g.; 14 * 15 * <code> 16 * if ( utf8_is_ascii($someString) ) { 17 * // It's just ASCII - use the native PHP version 18 * $someString = strtolower($someString); 19 * } else { 20 * $someString = utf8_strtolower($someString); 21 * } 22 * </code> 23 * 24 * @param string 25 * @return boolean TRUE if it's all ASCII 26 * @package utf8 27 * @see utf8_is_ascii_ctrl 28 */ 29 function utf8_is_ascii($str) { 30 // Search for any bytes which are outside the ASCII range... 31 return (preg_match('/(?:[^\x00-\x7F])/',$str) !== 1); 32 } 33 34 //-------------------------------------------------------------------- 35 /** 36 * Tests whether a string contains only 7bit ASCII bytes with device 37 * control codes omitted. The device control codes can be found on the 38 * second table here: http://www.w3schools.com/tags/ref_ascii.asp 39 * 40 * @param string 41 * @return boolean TRUE if it's all ASCII without device control codes 42 * @package utf8 43 * @see utf8_is_ascii 44 */ 45 function utf8_is_ascii_ctrl($str) { 46 if ( strlen($str) > 0 ) { 47 // Search for any bytes which are outside the ASCII range, 48 // or are device control codes 49 return (preg_match('/[^\x09\x0A\x0D\x20-\x7E]/',$str) !== 1); 50 } 51 return FALSE; 52 } 53 54 //-------------------------------------------------------------------- 55 /** 56 * Strip out all non-7bit ASCII bytes 57 * If you need to transmit a string to system which you know can only 58 * support 7bit ASCII, you could use this function. 59 * @param string 60 * @return string with non ASCII bytes removed 61 * @package utf8 62 * @see utf8_strip_non_ascii_ctrl 63 */ 64 function utf8_strip_non_ascii($str) { 65 ob_start(); 66 while ( preg_match( 67 '/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S', 68 $str, $matches) ) { 69 if ( !isset($matches[2]) ) { 70 echo $matches[0]; 71 } 72 $str = substr($str, strlen($matches[0])); 73 } 74 $result = ob_get_contents(); 75 ob_end_clean(); 76 return $result; 77 } 78 79 //-------------------------------------------------------------------- 80 /** 81 * Strip out device control codes in the ASCII range 82 * which are not permitted in XML. Note that this leaves 83 * multi-byte characters untouched - it only removes device 84 * control codes 85 * @see http://hsivonen.iki.fi/producing-xml/#controlchar 86 * @param string 87 * @return string control codes removed 88 */ 89 function utf8_strip_ascii_ctrl($str) { 90 ob_start(); 91 while ( preg_match( 92 '/^([^\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)|([\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)/S', 93 $str, $matches) ) { 94 if ( !isset($matches[2]) ) { 95 echo $matches[0]; 96 } 97 $str = substr($str, strlen($matches[0])); 98 } 99 $result = ob_get_contents(); 100 ob_end_clean(); 101 return $result; 102 } 103 104 //-------------------------------------------------------------------- 105 /** 106 * Strip out all non 7bit ASCII bytes and ASCII device control codes. 107 * For a list of ASCII device control codes see the 2nd table here: 108 * http://www.w3schools.com/tags/ref_ascii.asp 109 * 110 * @param string 111 * @return boolean TRUE if it's all ASCII 112 * @package utf8 113 */ 114 function utf8_strip_non_ascii_ctrl($str) { 115 ob_start(); 116 while ( preg_match( 117 '/^([\x09\x0A\x0D\x20-\x7E]+)|([^\x09\x0A\x0D\x20-\x7E]+)/S', 118 $str, $matches) ) { 119 if ( !isset($matches[2]) ) { 120 echo $matches[0]; 121 } 122 $str = substr($str, strlen($matches[0])); 123 } 124 $result = ob_get_contents(); 125 ob_end_clean(); 126 return $result; 127 } 128 129 //--------------------------------------------------------------- 130 /** 131 * Replace accented UTF-8 characters by unaccented ASCII-7 "equivalents". 132 * The purpose of this function is to replace characters commonly found in Latin 133 * alphabets with something more or less equivalent from the ASCII range. This can 134 * be useful for converting a UTF-8 to something ready for a filename, for example. 135 * Following the use of this function, you would probably also pass the string 136 * through utf8_strip_non_ascii to clean out any other non-ASCII chars 137 * Use the optional parameter to just deaccent lower ($case = -1) or upper ($case = 1) 138 * letters. Default is to deaccent both cases ($case = 0) 139 * 140 * For a more complete implementation of transliteration, see the utf8_to_ascii package 141 * available from the phputf8 project downloads: 142 * http://prdownloads.sourceforge.net/phputf8 143 * 144 * @param string UTF-8 string 145 * @param int (optional) -1 lowercase only, +1 uppercase only, 1 both cases 146 * @param string UTF-8 with accented characters replaced by ASCII chars 147 * @return string accented chars replaced with ascii equivalents 148 * @author Andreas Gohr <[email protected]> 149 * @package utf8 150 */ 151 function utf8_accents_to_ascii( $str, $case=0 ){ 152 153 static $UTF8_LOWER_ACCENTS = NULL; 154 static $UTF8_UPPER_ACCENTS = NULL; 155 156 if($case <= 0){ 157 158 if ( is_null($UTF8_LOWER_ACCENTS) ) { 159 $UTF8_LOWER_ACCENTS = array( 160 'à' => 'a', 'ô' => 'o', 'ď' => 'd', 'ḟ' => 'f', 'ë' => 'e', 'š' => 's', 'ơ' => 'o', 161 'ß' => 'ss', 'ă' => 'a', 'ř' => 'r', 'ț' => 't', 'ň' => 'n', 'ā' => 'a', 'ķ' => 'k', 162 'ŝ' => 's', 'ỳ' => 'y', 'ņ' => 'n', 'ĺ' => 'l', 'ħ' => 'h', 'ṗ' => 'p', 'ó' => 'o', 163 'ú' => 'u', 'ě' => 'e', 'é' => 'e', 'ç' => 'c', 'ẁ' => 'w', 'ċ' => 'c', 'õ' => 'o', 164 'ṡ' => 's', 'ø' => 'o', 'ģ' => 'g', 'ŧ' => 't', 'ș' => 's', 'ė' => 'e', 'ĉ' => 'c', 165 'ś' => 's', 'î' => 'i', 'ű' => 'u', 'ć' => 'c', 'ę' => 'e', 'ŵ' => 'w', 'ṫ' => 't', 166 'ū' => 'u', 'č' => 'c', 'ö' => 'oe', 'è' => 'e', 'ŷ' => 'y', 'ą' => 'a', 'ł' => 'l', 167 'ų' => 'u', 'ů' => 'u', 'ş' => 's', 'ğ' => 'g', 'ļ' => 'l', 'ƒ' => 'f', 'ž' => 'z', 168 'ẃ' => 'w', 'ḃ' => 'b', 'å' => 'a', 'ì' => 'i', 'ï' => 'i', 'ḋ' => 'd', 'ť' => 't', 169 'ŗ' => 'r', 'ä' => 'ae', 'í' => 'i', 'ŕ' => 'r', 'ê' => 'e', 'ü' => 'ue', 'ò' => 'o', 170 'ē' => 'e', 'ñ' => 'n', 'ń' => 'n', 'ĥ' => 'h', 'ĝ' => 'g', 'đ' => 'd', 'ĵ' => 'j', 171 'ÿ' => 'y', 'ũ' => 'u', 'ŭ' => 'u', 'ư' => 'u', 'ţ' => 't', 'ý' => 'y', 'ő' => 'o', 172 'â' => 'a', 'ľ' => 'l', 'ẅ' => 'w', 'ż' => 'z', 'ī' => 'i', 'ã' => 'a', 'ġ' => 'g', 173 'ṁ' => 'm', 'ō' => 'o', 'ĩ' => 'i', 'ù' => 'u', 'į' => 'i', 'ź' => 'z', 'á' => 'a', 174 'û' => 'u', 'þ' => 'th', 'ð' => 'dh', 'æ' => 'ae', 'µ' => 'u', 'ĕ' => 'e', 175 ); 176 } 177 178 $str = str_replace( 179 array_keys($UTF8_LOWER_ACCENTS), 180 array_values($UTF8_LOWER_ACCENTS), 181 $str 182 ); 183 } 184 185 if($case >= 0){ 186 if ( is_null($UTF8_UPPER_ACCENTS) ) { 187 $UTF8_UPPER_ACCENTS = array( 188 'À' => 'A', 'Ô' => 'O', 'Ď' => 'D', 'Ḟ' => 'F', 'Ë' => 'E', 'Š' => 'S', 'Ơ' => 'O', 189 'Ă' => 'A', 'Ř' => 'R', 'Ț' => 'T', 'Ň' => 'N', 'Ā' => 'A', 'Ķ' => 'K', 190 'Ŝ' => 'S', 'Ỳ' => 'Y', 'Ņ' => 'N', 'Ĺ' => 'L', 'Ħ' => 'H', 'Ṗ' => 'P', 'Ó' => 'O', 191 'Ú' => 'U', 'Ě' => 'E', 'É' => 'E', 'Ç' => 'C', 'Ẁ' => 'W', 'Ċ' => 'C', 'Õ' => 'O', 192 'Ṡ' => 'S', 'Ø' => 'O', 'Ģ' => 'G', 'Ŧ' => 'T', 'Ș' => 'S', 'Ė' => 'E', 'Ĉ' => 'C', 193 'Ś' => 'S', 'Î' => 'I', 'Ű' => 'U', 'Ć' => 'C', 'Ę' => 'E', 'Ŵ' => 'W', 'Ṫ' => 'T', 194 'Ū' => 'U', 'Č' => 'C', 'Ö' => 'Oe', 'È' => 'E', 'Ŷ' => 'Y', 'Ą' => 'A', 'Ł' => 'L', 195 'Ų' => 'U', 'Ů' => 'U', 'Ş' => 'S', 'Ğ' => 'G', 'Ļ' => 'L', 'Ƒ' => 'F', 'Ž' => 'Z', 196 'Ẃ' => 'W', 'Ḃ' => 'B', 'Å' => 'A', 'Ì' => 'I', 'Ï' => 'I', 'Ḋ' => 'D', 'Ť' => 'T', 197 'Ŗ' => 'R', 'Ä' => 'Ae', 'Í' => 'I', 'Ŕ' => 'R', 'Ê' => 'E', 'Ü' => 'Ue', 'Ò' => 'O', 198 'Ē' => 'E', 'Ñ' => 'N', 'Ń' => 'N', 'Ĥ' => 'H', 'Ĝ' => 'G', 'Đ' => 'D', 'Ĵ' => 'J', 199 'Ÿ' => 'Y', 'Ũ' => 'U', 'Ŭ' => 'U', 'Ư' => 'U', 'Ţ' => 'T', 'Ý' => 'Y', 'Ő' => 'O', 200 'Â' => 'A', 'Ľ' => 'L', 'Ẅ' => 'W', 'Ż' => 'Z', 'Ī' => 'I', 'Ã' => 'A', 'Ġ' => 'G', 201 'Ṁ' => 'M', 'Ō' => 'O', 'Ĩ' => 'I', 'Ù' => 'U', 'Į' => 'I', 'Ź' => 'Z', 'Á' => 'A', 202 'Û' => 'U', 'Þ' => 'Th', 'Ð' => 'Dh', 'Æ' => 'Ae', 'Ĕ' => 'E', 203 ); 204 } 205 $str = str_replace( 206 array_keys($UTF8_UPPER_ACCENTS), 207 array_values($UTF8_UPPER_ACCENTS), 208 $str 209 ); 210 } 211 212 return $str; 213 214 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |