| 1 |
552
|
thorn
|
<?php
|
| 2 |
|
|
|
| 3 |
554
|
Ruebenwurz
|
// $Id$
|
| 4 |
552
|
thorn
|
|
| 5 |
|
|
/*
|
| 6 |
|
|
|
| 7 |
|
|
Website Baker Project <http://www.websitebaker.org/>
|
| 8 |
915
|
Ruebenwurz
|
Copyright (C) 2004-2009, Ryan Djurovich
|
| 9 |
552
|
thorn
|
|
| 10 |
|
|
Website Baker is free software; you can redistribute it and/or modify
|
| 11 |
|
|
it under the terms of the GNU General Public License as published by
|
| 12 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
| 13 |
|
|
(at your option) any later version.
|
| 14 |
|
|
|
| 15 |
|
|
Website Baker is distributed in the hope that it will be useful,
|
| 16 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 17 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 18 |
|
|
GNU General Public License for more details.
|
| 19 |
|
|
|
| 20 |
|
|
You should have received a copy of the GNU General Public License
|
| 21 |
|
|
along with Website Baker; if not, write to the Free Software
|
| 22 |
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 23 |
|
|
|
| 24 |
|
|
*/
|
| 25 |
|
|
|
| 26 |
|
|
/*
|
| 27 |
761
|
thorn
|
* A part of this file is based on 'utf8.php' from the DokuWiki-project.
|
| 28 |
552
|
thorn
|
* (http://www.splitbrain.org/projects/dokuwiki):
|
| 29 |
|
|
**
|
| 30 |
|
|
* UTF8 helper functions
|
| 31 |
|
|
* @license LGPL (http://www.gnu.org/copyleft/lesser.html)
|
| 32 |
|
|
* @author Andreas Gohr <andi@splitbrain.org>
|
| 33 |
|
|
**
|
| 34 |
|
|
* modified for use with Website Baker
|
| 35 |
|
|
* from thorn, Jan. 2008
|
| 36 |
761
|
thorn
|
*
|
| 37 |
|
|
* most of the original functions appeared to be to slow with large strings, so i replaced them with my own ones
|
| 38 |
|
|
* thorn, Mar. 2008
|
| 39 |
552
|
thorn
|
*/
|
| 40 |
|
|
|
| 41 |
|
|
// Functions we use in Website Baker:
|
| 42 |
|
|
// entities_to_7bit()
|
| 43 |
|
|
// entities_to_umlauts2()
|
| 44 |
|
|
// umlauts_to_entities2()
|
| 45 |
|
|
|
| 46 |
|
|
if(!defined('WB_URL')) {
|
| 47 |
|
|
header('Location: ../index.php');
|
| 48 |
|
|
exit(0);
|
| 49 |
|
|
}
|
| 50 |
|
|
|
| 51 |
|
|
/*
|
| 52 |
|
|
* check for mb_string support
|
| 53 |
|
|
*/
|
| 54 |
761
|
thorn
|
//define('UTF8_NOMBSTRING',1); // uncomment this to forbid use of mb_string-functions
|
| 55 |
552
|
thorn
|
if(!defined('UTF8_MBSTRING')){
|
| 56 |
|
|
if(function_exists('mb_substr') && !defined('UTF8_NOMBSTRING')){
|
| 57 |
|
|
define('UTF8_MBSTRING',1);
|
| 58 |
|
|
}else{
|
| 59 |
|
|
define('UTF8_MBSTRING',0);
|
| 60 |
|
|
}
|
| 61 |
|
|
}
|
| 62 |
|
|
|
| 63 |
|
|
if(UTF8_MBSTRING){ mb_internal_encoding('UTF-8'); }
|
| 64 |
|
|
|
| 65 |
|
|
require_once(WB_PATH.'/framework/charsets_table.php');
|
| 66 |
|
|
|
| 67 |
|
|
/*
|
| 68 |
|
|
* Checks if a string contains 7bit ASCII only
|
| 69 |
|
|
*
|
| 70 |
758
|
thorn
|
* @author thorn
|
| 71 |
552
|
thorn
|
*/
|
| 72 |
|
|
function utf8_isASCII($str){
|
| 73 |
758
|
thorn
|
if(preg_match('/[\x80-\xFF]/', $str))
|
| 74 |
|
|
return false;
|
| 75 |
|
|
else
|
| 76 |
|
|
return true;
|
| 77 |
552
|
thorn
|
}
|
| 78 |
|
|
|
| 79 |
|
|
/*
|
| 80 |
|
|
* Tries to detect if a string is in Unicode encoding
|
| 81 |
|
|
*
|
| 82 |
|
|
* @author <bmorel@ssi.fr>
|
| 83 |
|
|
* @link http://www.php.net/manual/en/function.utf8-encode.php
|
| 84 |
|
|
*/
|
| 85 |
|
|
function utf8_check($Str) {
|
| 86 |
|
|
for ($i=0; $i<strlen($Str); $i++) {
|
| 87 |
|
|
$b = ord($Str[$i]);
|
| 88 |
|
|
if ($b < 0x80) continue; # 0bbbbbbb
|
| 89 |
|
|
elseif (($b & 0xE0) == 0xC0) $n=1; # 110bbbbb
|
| 90 |
|
|
elseif (($b & 0xF0) == 0xE0) $n=2; # 1110bbbb
|
| 91 |
|
|
elseif (($b & 0xF8) == 0xF0) $n=3; # 11110bbb
|
| 92 |
|
|
elseif (($b & 0xFC) == 0xF8) $n=4; # 111110bb
|
| 93 |
|
|
elseif (($b & 0xFE) == 0xFC) $n=5; # 1111110b
|
| 94 |
|
|
else return false; # Does not match any model
|
| 95 |
|
|
for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
|
| 96 |
|
|
if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
|
| 97 |
|
|
return false;
|
| 98 |
|
|
}
|
| 99 |
|
|
}
|
| 100 |
|
|
return true;
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
/*
|
| 104 |
|
|
* Romanize a non-latin string
|
| 105 |
|
|
*
|
| 106 |
|
|
* @author Andreas Gohr <andi@splitbrain.org>
|
| 107 |
|
|
*/
|
| 108 |
|
|
function utf8_romanize($string){
|
| 109 |
|
|
if(utf8_isASCII($string)) return $string; //nothing to do
|
| 110 |
|
|
|
| 111 |
|
|
global $UTF8_ROMANIZATION;
|
| 112 |
|
|
return strtr($string,$UTF8_ROMANIZATION);
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
/*
|
| 116 |
|
|
* Removes special characters (nonalphanumeric) from a UTF-8 string
|
| 117 |
|
|
*
|
| 118 |
|
|
* This function adds the controlchars 0x00 to 0x19 to the array of
|
| 119 |
|
|
* stripped chars (they are not included in $UTF8_SPECIAL_CHARS2)
|
| 120 |
|
|
*
|
| 121 |
|
|
* @author Andreas Gohr <andi@splitbrain.org>
|
| 122 |
|
|
* @param string $string The UTF8 string to strip of special chars
|
| 123 |
|
|
* @param string $repl Replace special with this string
|
| 124 |
|
|
* @param string $additional Additional chars to strip (used in regexp char class)
|
| 125 |
|
|
*/
|
| 126 |
|
|
function utf8_stripspecials($string,$repl='',$additional=''){
|
| 127 |
|
|
global $UTF8_SPECIAL_CHARS2;
|
| 128 |
|
|
|
| 129 |
|
|
static $specials = null;
|
| 130 |
|
|
if(is_null($specials)){
|
| 131 |
|
|
$specials = preg_quote($UTF8_SPECIAL_CHARS2, '/');
|
| 132 |
|
|
}
|
| 133 |
|
|
|
| 134 |
|
|
return preg_replace('/['.$additional.'\x00-\x19'.$specials.']/u',$repl,$string);
|
| 135 |
|
|
}
|
| 136 |
|
|
|
| 137 |
|
|
/*
|
| 138 |
761
|
thorn
|
* added functions - thorn
|
| 139 |
552
|
thorn
|
*/
|
| 140 |
|
|
|
| 141 |
|
|
/*
|
| 142 |
761
|
thorn
|
* faster replacement for utf8_entities_to_umlauts()
|
| 143 |
|
|
* not all features of utf8_entities_to_umlauts() --> utf8_unhtml() are supported!
|
| 144 |
|
|
* @author thorn
|
| 145 |
552
|
thorn
|
*/
|
| 146 |
761
|
thorn
|
function utf8_fast_entities_to_umlauts($str) {
|
| 147 |
|
|
if(UTF8_MBSTRING) {
|
| 148 |
|
|
// we need this for use with mb_convert_encoding
|
| 149 |
881
|
thorn
|
$str = str_replace(array('&','>','<','"',''',' '), array('&amp;','&gt;','&lt;','&quot;','&#39;','&nbsp;'), $str);
|
| 150 |
761
|
thorn
|
// we need two mb_convert_encoding()-calls - is this a bug?
|
| 151 |
|
|
// mb_convert_encoding("öö", 'UTF-8', 'HTML-ENTITIES'); // with string in utf-8-encoding doesn't work. Result: "öö"
|
| 152 |
|
|
// Work-around: convert all umlauts to entities first ("öö"->"öö"), then all entities to umlauts ("öö"->"öö")
|
| 153 |
|
|
return(mb_convert_encoding(mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8'),'UTF-8', 'HTML-ENTITIES'));
|
| 154 |
|
|
} else {
|
| 155 |
|
|
global $named_entities;global $numbered_entities;
|
| 156 |
|
|
$str = str_replace($named_entities, $numbered_entities, $str);
|
| 157 |
|
|
$str = preg_replace("/&#([0-9]+);/e", "code_to_utf8($1)", $str);
|
| 158 |
|
|
}
|
| 159 |
|
|
return($str);
|
| 160 |
552
|
thorn
|
}
|
| 161 |
761
|
thorn
|
// support-function for utf8_fast_entities_to_umlauts()
|
| 162 |
|
|
function code_to_utf8($num) {
|
| 163 |
|
|
if ($num <= 0x7F) {
|
| 164 |
|
|
return chr($num);
|
| 165 |
|
|
} elseif ($num <= 0x7FF) {
|
| 166 |
|
|
return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
|
| 167 |
|
|
} elseif ($num <= 0xFFFF) {
|
| 168 |
|
|
return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
|
| 169 |
|
|
} elseif ($num <= 0x1FFFFF) {
|
| 170 |
|
|
return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
|
| 171 |
|
|
}
|
| 172 |
|
|
return "?";
|
| 173 |
552
|
thorn
|
}
|
| 174 |
|
|
|
| 175 |
|
|
/*
|
| 176 |
761
|
thorn
|
* faster replacement for utf8_umlauts_to_entities()
|
| 177 |
|
|
* not all features of utf8_umlauts_to_entities() --> utf8_tohtml() are supported!
|
| 178 |
|
|
* @author thorn
|
| 179 |
552
|
thorn
|
*/
|
| 180 |
761
|
thorn
|
function utf8_fast_umlauts_to_entities($string, $named_entities=true) {
|
| 181 |
|
|
if(UTF8_MBSTRING)
|
| 182 |
|
|
return(mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8'));
|
| 183 |
|
|
else {
|
| 184 |
|
|
global $named_entities;global $numbered_entities;
|
| 185 |
|
|
$new = "";
|
| 186 |
|
|
$i=0;
|
| 187 |
|
|
$len=strlen($string);
|
| 188 |
|
|
if($len==0) return $string;
|
| 189 |
|
|
do {
|
| 190 |
|
|
if(ord($string{$i}) <= 127) $ud = $string{$i++};
|
| 191 |
|
|
elseif(ord($string{$i}) <= 223) $ud = (ord($string{$i++})-192)*64 + (ord($string{$i++})-128);
|
| 192 |
|
|
elseif(ord($string{$i}) <= 239) $ud = (ord($string{$i++})-224)*4096 + (ord($string{$i++})-128)*64 + (ord($string{$i++})-128);
|
| 193 |
|
|
elseif(ord($string{$i}) <= 247) $ud = (ord($string{$i++})-240)*262144 + (ord($string{$i++})-128)*4096 + (ord($string{$i++})-128)*64 + (ord($string{$i++})-128);
|
| 194 |
|
|
else $ud = ord($string{$i++}); // error!
|
| 195 |
|
|
if($ud > 127) {
|
| 196 |
|
|
$new .= "&#$ud;";
|
| 197 |
|
|
} else {
|
| 198 |
|
|
$new .= $ud;
|
| 199 |
|
|
}
|
| 200 |
|
|
} while($i < $len);
|
| 201 |
|
|
$string = $new;
|
| 202 |
|
|
if($named_entities)
|
| 203 |
|
|
$string = str_replace($numbered_entities, $named_entities, $string);
|
| 204 |
|
|
}
|
| 205 |
|
|
return($string);
|
| 206 |
552
|
thorn
|
}
|
| 207 |
|
|
|
| 208 |
|
|
/*
|
| 209 |
|
|
* Converts from various charsets to UTF-8
|
| 210 |
|
|
*
|
| 211 |
|
|
* Will convert a string from various charsets to UTF-8.
|
| 212 |
761
|
thorn
|
* HTML-entities may be converted, too.
|
| 213 |
552
|
thorn
|
* In case of error the returned string is unchanged, and a message is emitted.
|
| 214 |
|
|
* Supported charsets are:
|
| 215 |
|
|
* direct: iso_8859_1 iso_8859_2 iso_8859_3 iso_8859_4 iso_8859_5
|
| 216 |
|
|
* iso_8859_6 iso_8859_7 iso_8859_8 iso_8859_9 iso_8859_10 iso_8859_11
|
| 217 |
|
|
* mb_convert_encoding: all wb charsets (except those from 'direct'); but not GB2312
|
| 218 |
|
|
* iconv: all wb charsets (except those from 'direct')
|
| 219 |
|
|
*
|
| 220 |
|
|
* @param string A string in supported encoding
|
| 221 |
|
|
* @param string The charset to convert from, defaults to DEFAULT_CHARSET
|
| 222 |
|
|
* @return string A string in UTF-8-encoding, with all entities decoded, too.
|
| 223 |
|
|
* String is unchanged in case of error.
|
| 224 |
761
|
thorn
|
* @author thorn
|
| 225 |
552
|
thorn
|
*/
|
| 226 |
761
|
thorn
|
function charset_to_utf8($str, $charset_in=DEFAULT_CHARSET, $decode_entities=true) {
|
| 227 |
552
|
thorn
|
global $iso_8859_2_to_utf8, $iso_8859_3_to_utf8, $iso_8859_4_to_utf8, $iso_8859_5_to_utf8, $iso_8859_6_to_utf8, $iso_8859_7_to_utf8, $iso_8859_8_to_utf8, $iso_8859_9_to_utf8, $iso_8859_10_to_utf8, $iso_8859_11_to_utf8;
|
| 228 |
|
|
$charset_in = strtoupper($charset_in);
|
| 229 |
|
|
if ($charset_in == "") { $charset_in = 'UTF-8'; }
|
| 230 |
|
|
$wrong_ISO8859 = false;
|
| 231 |
|
|
$converted = false;
|
| 232 |
|
|
|
| 233 |
782
|
thorn
|
if((!function_exists('iconv') && !UTF8_MBSTRING && ($charset_in=='BIG5' || $charset_in=='ISO-2022-JP' || $charset_in=='ISO-2022-KR')) || (!function_exists('iconv') && $charset_in=='GB2312')) {
|
| 234 |
552
|
thorn
|
// Nothing we can do here :-(
|
| 235 |
|
|
// Charset is one of those obscure ISO-2022... or BIG5, GB2312 or something
|
| 236 |
|
|
// and we can't use mb_convert_encoding() or iconv();
|
| 237 |
|
|
// Emit an error-message.
|
| 238 |
|
|
trigger_error("Can't convert from $charset_in without mb_convert_encoding() or iconv(). Use UTF-8 instead.", E_USER_WARNING);
|
| 239 |
|
|
return($str);
|
| 240 |
|
|
}
|
| 241 |
|
|
|
| 242 |
|
|
// check if we have UTF-8 or a plain ASCII string
|
| 243 |
|
|
if($charset_in == 'UTF-8' || utf8_isASCII($str)) {
|
| 244 |
|
|
// we have utf-8. Just replace HTML-entities and return
|
| 245 |
761
|
thorn
|
if($decode_entities && preg_match('/&[#0-9a-zA-Z]+;/',$str))
|
| 246 |
|
|
return(utf8_fast_entities_to_umlauts($str));
|
| 247 |
552
|
thorn
|
else // nothing to do
|
| 248 |
|
|
return($str);
|
| 249 |
|
|
}
|
| 250 |
|
|
|
| 251 |
|
|
// Convert $str to utf8
|
| 252 |
|
|
if(substr($charset_in,0,8) == 'ISO-8859') {
|
| 253 |
|
|
switch($charset_in) {
|
| 254 |
|
|
case 'ISO-8859-1': $str=utf8_encode($str); break;
|
| 255 |
|
|
case 'ISO-8859-2': $str=strtr($str, $iso_8859_2_to_utf8); break;
|
| 256 |
|
|
case 'ISO-8859-3': $str=strtr($str, $iso_8859_3_to_utf8); break;
|
| 257 |
|
|
case 'ISO-8859-4': $str=strtr($str, $iso_8859_4_to_utf8); break;
|
| 258 |
|
|
case 'ISO-8859-5': $str=strtr($str, $iso_8859_5_to_utf8); break;
|
| 259 |
|
|
case 'ISO-8859-6': $str=strtr($str, $iso_8859_6_to_utf8); break;
|
| 260 |
|
|
case 'ISO-8859-7': $str=strtr($str, $iso_8859_7_to_utf8); break;
|
| 261 |
|
|
case 'ISO-8859-8': $str=strtr($str, $iso_8859_8_to_utf8); break;
|
| 262 |
|
|
case 'ISO-8859-9': $str=strtr($str, $iso_8859_9_to_utf8); break;
|
| 263 |
|
|
case 'ISO-8859-10': $str=strtr($str, $iso_8859_10_to_utf8); break;
|
| 264 |
|
|
case 'ISO-8859-11': $str=strtr($str, $iso_8859_11_to_utf8); break;
|
| 265 |
|
|
default: $wrong_ISO8859 = true;
|
| 266 |
|
|
}
|
| 267 |
|
|
if(!$wrong_ISO8859)
|
| 268 |
|
|
$converted = true;
|
| 269 |
|
|
}
|
| 270 |
|
|
if(!$converted && UTF8_MBSTRING && $charset_in != 'GB2312') {
|
| 271 |
|
|
// $charset is neither UTF-8 nor a known ISO-8859...
|
| 272 |
|
|
// Try mb_convert_encoding() - but there's no GB2312 encoding in php's mb_* functions
|
| 273 |
|
|
$str = mb_convert_encoding($str, 'UTF-8', $charset_in);
|
| 274 |
|
|
$converted = true;
|
| 275 |
|
|
} elseif(!$converted) { // Try iconv
|
| 276 |
|
|
if(function_exists('iconv')) {
|
| 277 |
|
|
$str = iconv($charset_in, 'UTF-8', $str);
|
| 278 |
|
|
$converted = true;
|
| 279 |
|
|
}
|
| 280 |
|
|
}
|
| 281 |
|
|
if($converted) {
|
| 282 |
|
|
// we have utf-8, now replace HTML-entities and return
|
| 283 |
761
|
thorn
|
if($decode_entities && preg_match('/&[#0-9a-zA-Z]+;/',$str))
|
| 284 |
|
|
$str = utf8_fast_entities_to_umlauts($str);
|
| 285 |
552
|
thorn
|
return($str);
|
| 286 |
|
|
}
|
| 287 |
|
|
|
| 288 |
|
|
// Nothing we can do here :-(
|
| 289 |
|
|
// Charset is one of those obscure ISO-2022... or BIG5, GB2312 or something
|
| 290 |
|
|
// and we can't use mb_convert_encoding() or iconv();
|
| 291 |
|
|
// Emit an error-message.
|
| 292 |
|
|
trigger_error("Can't convert from $charset_in without mb_convert_encoding() or iconv(). Use UTF-8 instead.", E_USER_WARNING);
|
| 293 |
|
|
|
| 294 |
|
|
return $str;
|
| 295 |
|
|
}
|
| 296 |
|
|
|
| 297 |
|
|
/*
|
| 298 |
|
|
* Converts from UTF-8 to various charsets
|
| 299 |
|
|
*
|
| 300 |
|
|
* Will convert a string from UTF-8 to various charsets.
|
| 301 |
761
|
thorn
|
* HTML-entities will not! be converted.
|
| 302 |
552
|
thorn
|
* In case of error the returned string is unchanged, and a message is emitted.
|
| 303 |
|
|
* Supported charsets are:
|
| 304 |
|
|
* direct: iso_8859_1 iso_8859_2 iso_8859_3 iso_8859_4 iso_8859_5
|
| 305 |
|
|
* iso_8859_6 iso_8859_7 iso_8859_8 iso_8859_9 iso_8859_10 iso_8859_11
|
| 306 |
|
|
* mb_convert_encoding: all wb charsets (except those from 'direct'); but not GB2312
|
| 307 |
|
|
* iconv: all wb charsets (except those from 'direct')
|
| 308 |
|
|
*
|
| 309 |
|
|
* @param string An UTF-8 encoded string
|
| 310 |
|
|
* @param string The charset to convert to, defaults to DEFAULT_CHARSET
|
| 311 |
|
|
* @return string A string in a supported encoding, with all entities decoded, too.
|
| 312 |
|
|
* String is unchanged in case of error.
|
| 313 |
761
|
thorn
|
* @author thorn
|
| 314 |
552
|
thorn
|
*/
|
| 315 |
|
|
function utf8_to_charset($str, $charset_out=DEFAULT_CHARSET) {
|
| 316 |
|
|
global $utf8_to_iso_8859_2, $utf8_to_iso_8859_3, $utf8_to_iso_8859_4, $utf8_to_iso_8859_5, $utf8_to_iso_8859_6, $utf8_to_iso_8859_7, $utf8_to_iso_8859_8, $utf8_to_iso_8859_9, $utf8_to_iso_8859_10, $utf8_to_iso_8859_11;
|
| 317 |
|
|
$charset_out = strtoupper($charset_out);
|
| 318 |
|
|
$wrong_ISO8859 = false;
|
| 319 |
|
|
$converted = false;
|
| 320 |
|
|
|
| 321 |
782
|
thorn
|
if((!function_exists('iconv') && !UTF8_MBSTRING && ($charset_out=='BIG5' || $charset_out=='ISO-2022-JP' || $charset_out=='ISO-2022-KR')) || (!function_exists('iconv') && $charset_out=='GB2312')) {
|
| 322 |
552
|
thorn
|
// Nothing we can do here :-(
|
| 323 |
|
|
// Charset is one of those obscure ISO-2022... or BIG5, GB2312 or something
|
| 324 |
|
|
// and we can't use mb_convert_encoding() or iconv();
|
| 325 |
|
|
// Emit an error-message.
|
| 326 |
|
|
trigger_error("Can't convert into $charset_out without mb_convert_encoding() or iconv(). Use UTF-8 instead.", E_USER_WARNING);
|
| 327 |
|
|
return($str);
|
| 328 |
|
|
}
|
| 329 |
|
|
|
| 330 |
761
|
thorn
|
// the string comes from charset_to_utf8(), so we can skip this
|
| 331 |
552
|
thorn
|
// replace HTML-entities first
|
| 332 |
761
|
thorn
|
//if(preg_match('/&[#0-9a-zA-Z]+;/',$str))
|
| 333 |
|
|
// $str = utf8_entities_to_umlauts($str);
|
| 334 |
552
|
thorn
|
|
| 335 |
|
|
// check if we need to convert
|
| 336 |
|
|
if($charset_out == 'UTF-8' || utf8_isASCII($str)) {
|
| 337 |
|
|
// Nothing to do. Just return
|
| 338 |
|
|
return($str);
|
| 339 |
|
|
}
|
| 340 |
|
|
|
| 341 |
|
|
// Convert $str to $charset_out
|
| 342 |
|
|
if(substr($charset_out,0,8) == 'ISO-8859') {
|
| 343 |
|
|
switch($charset_out) {
|
| 344 |
|
|
case 'ISO-8859-1': $str=utf8_decode($str); break;
|
| 345 |
|
|
case 'ISO-8859-2': $str=strtr($str, $utf8_to_iso_8859_2); break;
|
| 346 |
|
|
case 'ISO-8859-3': $str=strtr($str, $utf8_to_iso_8859_3); break;
|
| 347 |
|
|
case 'ISO-8859-4': $str=strtr($str, $utf8_to_iso_8859_4); break;
|
| 348 |
|
|
case 'ISO-8859-5': $str=strtr($str, $utf8_to_iso_8859_5); break;
|
| 349 |
|
|
case 'ISO-8859-6': $str=strtr($str, $utf8_to_iso_8859_6); break;
|
| 350 |
|
|
case 'ISO-8859-7': $str=strtr($str, $utf8_to_iso_8859_7); break;
|
| 351 |
|
|
case 'ISO-8859-8': $str=strtr($str, $utf8_to_iso_8859_8); break;
|
| 352 |
|
|
case 'ISO-8859-9': $str=strtr($str, $utf8_to_iso_8859_9); break;
|
| 353 |
|
|
case 'ISO-8859-10': $str=strtr($str, $utf8_to_iso_8859_10); break;
|
| 354 |
|
|
case 'ISO-8859-11': $str=strtr($str, $utf8_to_iso_8859_11); break;
|
| 355 |
|
|
default: $wrong_ISO8859 = true;
|
| 356 |
|
|
}
|
| 357 |
|
|
if(!$wrong_ISO8859)
|
| 358 |
|
|
$converted = true;
|
| 359 |
|
|
}
|
| 360 |
|
|
if(!$converted && UTF8_MBSTRING && $charset_out != 'GB2312') {
|
| 361 |
|
|
// $charset is neither UTF-8 nor a known ISO-8859...
|
| 362 |
|
|
// Try mb_convert_encoding() - but there's no GB2312 encoding in php's mb_* functions
|
| 363 |
|
|
$str = mb_convert_encoding($str, $charset_out, 'UTF-8');
|
| 364 |
|
|
$converted = true;
|
| 365 |
|
|
} elseif(!$converted) { // Try iconv
|
| 366 |
|
|
if(function_exists('iconv')) {
|
| 367 |
|
|
$str = iconv('UTF-8', $charset_out, $str);
|
| 368 |
|
|
$converted = true;
|
| 369 |
|
|
}
|
| 370 |
|
|
}
|
| 371 |
|
|
if($converted) {
|
| 372 |
|
|
return($str);
|
| 373 |
|
|
}
|
| 374 |
|
|
|
| 375 |
|
|
// Nothing we can do here :-(
|
| 376 |
|
|
// Charset is one of those obscure ISO-2022... or BIG5, GB2312 or something
|
| 377 |
|
|
// and we can't use mb_convert_encoding() or iconv();
|
| 378 |
|
|
// Emit an error-message.
|
| 379 |
|
|
trigger_error("Can't convert into $charset_out without mb_convert_encoding() or iconv(). Use UTF-8 instead.", E_USER_WARNING);
|
| 380 |
|
|
|
| 381 |
|
|
return $str;
|
| 382 |
|
|
}
|
| 383 |
|
|
|
| 384 |
|
|
/*
|
| 385 |
|
|
* convert Filenames to ASCII
|
| 386 |
|
|
*
|
| 387 |
|
|
* Convert all non-ASCII characters and all HTML-entities to their plain 7bit equivalents
|
| 388 |
|
|
* Characters without an equivalent will be converted to hex-values.
|
| 389 |
|
|
* The name entities_to_7bit() is somewhat misleading, but kept for compatibility-reasons.
|
| 390 |
|
|
*
|
| 391 |
|
|
* @param string Filename to convert (all encodings from charset_to_utf8() are allowed)
|
| 392 |
|
|
* @return string ASCII encoded string, to use as filename in wb's page_filename() and media_filename
|
| 393 |
761
|
thorn
|
* @author thorn
|
| 394 |
552
|
thorn
|
*/
|
| 395 |
|
|
function entities_to_7bit($str) {
|
| 396 |
|
|
// convert to UTF-8
|
| 397 |
|
|
$str = charset_to_utf8($str);
|
| 398 |
731
|
thorn
|
if(!utf8_check($str))
|
| 399 |
|
|
return($str);
|
| 400 |
552
|
thorn
|
// replace some specials
|
| 401 |
|
|
$str = utf8_stripspecials($str, '_');
|
| 402 |
|
|
// translate non-ASCII characters to ASCII
|
| 403 |
|
|
$str = utf8_romanize($str);
|
| 404 |
|
|
// missed some? - Many UTF-8-chars can't be romanized
|
| 405 |
|
|
// convert to HTML-entities, and replace entites by hex-numbers
|
| 406 |
761
|
thorn
|
$str = utf8_fast_umlauts_to_entities($str, false);
|
| 407 |
881
|
thorn
|
$str = str_replace(''', ''', $str);
|
| 408 |
552
|
thorn
|
$str = preg_replace('/&#([0-9]+);/e', "dechex('$1')", $str);
|
| 409 |
|
|
// maybe there are some > < ' " & left, replace them too
|
| 410 |
881
|
thorn
|
$str = str_replace(array('>', '<', ''', '\'', '"', '&'), '', $str);
|
| 411 |
|
|
$str = str_replace('&', '', $str);
|
| 412 |
552
|
thorn
|
|
| 413 |
|
|
return($str);
|
| 414 |
|
|
}
|
| 415 |
|
|
|
| 416 |
|
|
/*
|
| 417 |
|
|
* Convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
|
| 418 |
|
|
*
|
| 419 |
|
|
* Will replace all numeric and named entities except
|
| 420 |
881
|
thorn
|
* > < ' " '
|
| 421 |
761
|
thorn
|
* @author thorn
|
| 422 |
552
|
thorn
|
*/
|
| 423 |
|
|
function entities_to_umlauts2($string, $charset_out=DEFAULT_CHARSET) {
|
| 424 |
761
|
thorn
|
$string = charset_to_utf8($string, DEFAULT_CHARSET, true);
|
| 425 |
|
|
//if(utf8_check($string)) // this check is to much time-consuming (this may fail only if AddDefaultCharset is set)
|
| 426 |
552
|
thorn
|
$string = utf8_to_charset($string, $charset_out);
|
| 427 |
|
|
return ($string);
|
| 428 |
|
|
}
|
| 429 |
|
|
|
| 430 |
|
|
/*
|
| 431 |
|
|
* Convert a string from mixed html-entities/umlauts to pure ASCII with HTML-entities
|
| 432 |
|
|
*
|
| 433 |
|
|
* Will convert a string in $charset_in encoding to a pure ASCII string with HTML-entities.
|
| 434 |
761
|
thorn
|
* @author thorn
|
| 435 |
552
|
thorn
|
*/
|
| 436 |
|
|
function umlauts_to_entities2($string, $charset_in=DEFAULT_CHARSET) {
|
| 437 |
761
|
thorn
|
$string = charset_to_utf8($string, $charset_in, false);
|
| 438 |
|
|
//if(utf8_check($string)) // this check is to much time-consuming (this may fail only if AddDefaultCharset is set)
|
| 439 |
|
|
$string = utf8_fast_umlauts_to_entities($string, false);
|
| 440 |
552
|
thorn
|
return($string);
|
| 441 |
|
|
}
|
| 442 |
|
|
|
| 443 |
758
|
thorn
|
?>
|