Changeset View
Changeset View
Standalone View
Standalone View
src/utils/utf8.php
Show First 20 Lines • Show All 666 Lines • ▼ Show 20 Lines | |||||
* This is primarily a thin wrapper around `mb_convert_encoding()` which checks | * This is primarily a thin wrapper around `mb_convert_encoding()` which checks | ||||
* you have the extension installed, since we try to require the extension | * you have the extension installed, since we try to require the extension | ||||
* only if you actually need it (i.e., you want to work with encodings other | * only if you actually need it (i.e., you want to work with encodings other | ||||
* than UTF-8). | * than UTF-8). | ||||
* | * | ||||
* NOTE: This function assumes that the input is in the given source encoding. | * NOTE: This function assumes that the input is in the given source encoding. | ||||
* If it is not, it may not output in the specified target encoding. If you | * If it is not, it may not output in the specified target encoding. If you | ||||
* need to perform a hard conversion to UTF-8, use this function in conjunction | * need to perform a hard conversion to UTF-8, use this function in conjunction | ||||
* with @{function:phutil_utf8ize}. We can detect failures caused by invalid | * with @{function:phutil_utf8ize}. We can detect failures caused by invalid | ||||
* encoding names, but `mb_convert_encoding()` fails silently if the | * encoding names, but `mb_convert_encoding()` fails silently if the | ||||
* encoding name identifies a real encoding but the string is not actually | * encoding name identifies a real encoding but the string is not actually | ||||
* encoded with that encoding. | * encoded with that encoding. | ||||
valerio.bozzolan: I don't get the old description.
> We can detect failures caused by invalid encoding names… | |||||
* | * | ||||
* @param string String to re-encode. | * @param string String to re-encode. | ||||
* @param string Target encoding name, like "UTF-8". | * @param string Target encoding name, like "UTF-8". | ||||
* @param string Source encoding name, like "ISO-8859-1". | * @param string Source encoding name, like "ISO-8859-1". | ||||
* @return string Input string, with converted character encoding. | * @return string Input string, with converted character encoding. | ||||
* | * | ||||
* @phutil-external-symbol function mb_convert_encoding | * @phutil-external-symbol function mb_convert_encoding | ||||
*/ | */ | ||||
Show All 26 Lines | throw new Exception( | ||||
"but the '%s' PHP extension is not available. Install %s to ". | "but the '%s' PHP extension is not available. Install %s to ". | ||||
"work with encodings other than UTF-8.", | "work with encodings other than UTF-8.", | ||||
$from_encoding, | $from_encoding, | ||||
$to_encoding, | $to_encoding, | ||||
'mbstring', | 'mbstring', | ||||
'mbstring')); | 'mbstring')); | ||||
} | } | ||||
$result = @mb_convert_encoding($string, $to_encoding, $from_encoding); | try { | ||||
$result = mb_convert_encoding($string, $to_encoding, $from_encoding); | |||||
if ($result === false) { | } catch (Throwable $ex) { | ||||
$message = error_get_last(); | $message = $ex->getMessage(); | ||||
if ($message) { | |||||
$message = idx($message, 'message', pht('Unknown error.')); | |||||
} | |||||
throw new Exception( | throw new Exception( | ||||
pht( | pht( | ||||
"String conversion from encoding '%s' to encoding '%s' failed: %s", | "String conversion from encoding '%s' to encoding '%s' failed: %s", | ||||
$from_encoding, | $from_encoding, | ||||
$to_encoding, | $to_encoding, | ||||
$message)); | $message)); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 258 Lines • Show Last 20 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0
I don't get the old description.
But?