Revision 483
Added by Matthias over 18 years ago
| functions.php | ||
|---|---|---|
| 348 | 348 |
|
| 349 | 349 |
// Function to convert a string from $from- to $to-encoding, using mysql |
| 350 | 350 |
function my_mysql_iconv($string, $from, $to) {
|
| 351 |
// keep current character set values: |
|
| 352 |
$character_set_database = mysql_result(mysql_query("SELECT @@character_set_client"),0,0);
|
|
| 353 |
$character_set_results = mysql_result(mysql_query("SELECT @@character_set_results"),0,0);
|
|
| 354 |
$collation_results = mysql_result(mysql_query("SELECT @@collation_connection"),0,0);
|
|
| 355 |
mysql_query("SET character_set_client=$from");
|
|
| 356 |
mysql_query("SET character_set_results=$to");
|
|
| 357 |
mysql_query("SET collation_connection=utf8_unicode_ci");
|
|
| 351 |
// keep current character set values |
|
| 352 |
global $database; |
|
| 353 |
$query = $database->query("SELECT @@character_set_client");
|
|
| 354 |
if($query->numRows() > 0) {
|
|
| 355 |
$res = $query->fetchRow(); |
|
| 356 |
$character_set_database = $res['@@character_set_client']; |
|
| 357 |
} else { echo mysql_error()."\n<br />"; }
|
|
| 358 |
$query = $database->query("SELECT @@character_set_results");
|
|
| 359 |
if($query->numRows() > 0) {
|
|
| 360 |
$res = $query->fetchRow(); |
|
| 361 |
$character_set_results = $res['@@character_set_results']; |
|
| 362 |
} else { echo mysql_error()."\n<br />"; }
|
|
| 363 |
$query = $database->query("SELECT @@collation_connection");
|
|
| 364 |
if($query->numRows() > 0) {
|
|
| 365 |
$res = $query->fetchRow(); |
|
| 366 |
$collation_results = $res['@@collation_connection']; |
|
| 367 |
} else { echo mysql_error()."\n<br />"; }
|
|
| 368 |
// set new character set values |
|
| 369 |
$query = $database->query("SET character_set_client=$from");
|
|
| 370 |
$query = $database->query("SET character_set_results=$to");
|
|
| 371 |
$query = $database->query("SET collation_connection=utf8_unicode_ci");
|
|
| 358 | 372 |
$string_escaped = mysql_real_escape_string($string); |
| 359 |
$converted_string = mysql_result(mysql_query("SELECT '$string_escaped'"),0,0);
|
|
| 360 |
// restore previous character set values: |
|
| 361 |
mysql_query("SET character_set_client=$character_set_database");
|
|
| 362 |
mysql_query("SET character_set_results=$character_set_results");
|
|
| 363 |
mysql_query("SET collation_connection=$collation_results");
|
|
| 373 |
// convert the string |
|
| 374 |
$query = $database->query("SELECT '$string_escaped'");
|
|
| 375 |
if($query->numRows() > 0) {
|
|
| 376 |
$res = $query->fetchRow(); |
|
| 377 |
$converted_string = $res[0]; |
|
| 378 |
} else { echo mysql_error()."\n<br />"; }
|
|
| 379 |
// restore previous character set values |
|
| 380 |
$query = $database->query("SET character_set_client=$character_set_database");
|
|
| 381 |
$query = $database->query("SET character_set_results=$character_set_results");
|
|
| 382 |
$query = $database->query("SET collation_connection=$collation_results");
|
|
| 364 | 383 |
return $converted_string; |
| 365 | 384 |
} |
| 366 | 385 |
|
| ... | ... | |
| 373 | 392 |
} |
| 374 | 393 |
$use_iconv = true; |
| 375 | 394 |
$use_mbstring = true; |
| 395 |
/* |
|
| 376 | 396 |
if(version_compare(PHP_VERSION, "5.1.0", "<")) {
|
| 377 | 397 |
$use_mbstring = false; // don't rely on mb_convert_encoding if php<5.1.0 |
| 378 | 398 |
$use_iconv = false; // don't rely on iconv neither |
| 379 | 399 |
} |
| 400 |
*/ |
|
| 380 | 401 |
|
| 381 | 402 |
// try mb_convert_encoding(). This can handle to or from HTML-ENTITIES, too |
| 382 | 403 |
if ($use_mbstring && function_exists('mb_convert_encoding')) {
|
| ... | ... | |
| 723 | 744 |
$string=utf8_encode($string); |
| 724 | 745 |
} |
| 725 | 746 |
// encode html-entities |
| 726 |
//$string=string_decode_encode_entities($string, 'HTML-ENTITIES', 'UTF-8');
|
|
| 727 |
$string=mb_convert_encoding_wrapper($string, 'HTML-ENTITIES', 'UTF-8'); |
|
| 747 |
$string=string_decode_encode_entities($string, 'HTML-ENTITIES', 'UTF-8'); // this is very slow!
|
|
| 748 |
//$string=mb_convert_encoding_wrapper($string, 'HTML-ENTITIES', 'UTF-8');
|
|
| 728 | 749 |
} |
| 729 | 750 |
else {
|
| 730 | 751 |
$string = string_to_utf8($string, $charset_in); |
| 731 | 752 |
// encode html-entities |
| 732 | 753 |
if (is_UTF8($string)) {
|
| 733 |
//$string=string_decode_encode_entities($string, 'HTML-ENTITIES', 'UTF-8');
|
|
| 734 |
$string=mb_convert_encoding_wrapper($string, 'HTML-ENTITIES', 'UTF-8'); |
|
| 754 |
$string=string_decode_encode_entities($string, 'HTML-ENTITIES', 'UTF-8'); |
|
| 755 |
//$string=mb_convert_encoding_wrapper($string, 'HTML-ENTITIES', 'UTF-8');
|
|
| 735 | 756 |
} |
| 736 | 757 |
} |
| 737 | 758 |
return $string; |
| 738 | 759 |
} |
| 739 | 760 |
|
| 761 |
function umlauts_to_defcharset($string, $charset) {
|
|
| 762 |
$charset_out = strtoupper(DEFAULT_CHARSET); |
|
| 763 |
if ($charset_out == "") { $charset_out = 'ISO-8859-1'; }
|
|
| 764 |
|
|
| 765 |
if($charset_out == $charset) {
|
|
| 766 |
return $string; |
|
| 767 |
} |
|
| 768 |
if($charset_out == 'ISO-8859-1' && $charset == 'UTF-8') {
|
|
| 769 |
$string = utf8_decode($string); |
|
| 770 |
} |
|
| 771 |
else {
|
|
| 772 |
$string=mb_convert_encoding_wrapper($string, $charset_out, 'UTF-8'); |
|
| 773 |
} |
|
| 774 |
|
|
| 775 |
return $string; |
|
| 776 |
} |
|
| 777 |
|
|
| 740 | 778 |
// translate any latin/greek/cyrillic html-entities to their plain 7bit equivalents |
| 741 | 779 |
// and numbered-entities into hex |
| 742 | 780 |
function entities_to_7bit($string) {
|
Also available in: Unified diff
Added changeset [482] to the branches