Changeset View
Changeset View
Standalone View
Standalone View
src/utils/utf8.php
Show First 20 Lines • Show All 283 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Find the character length of a UTF-8 string. | * Find the character length of a UTF-8 string. | ||||
* | * | ||||
* @param string A valid utf-8 string. | * @param string A valid utf-8 string. | ||||
* @return int The character length of the string. | * @return int The character length of the string. | ||||
*/ | */ | ||||
function phutil_utf8_strlen($string) { | function phutil_utf8_strlen($string) { | ||||
if (function_exists('utf8_decode')) { | if (function_exists('utf8_decode')) { | ||||
return strlen(utf8_decode($string)); | // Historically, this was just a call to strlen(utf8_decode($string)) | ||||
// but, since PHP 8.2, that function is deprecated, so this is | |||||
// the current equivalent. | |||||
// Note that we cannot just adopt mb_strlen($string, 'ISO-8859-1') | |||||
// since it's not the exact equivalent as far as we can see. | |||||
// https://we.phorge.it/T15188 | |||||
return strlen(mb_convert_encoding($string, 'ISO-8859-1')); | |||||
} | } | ||||
// TODO: Eventually drop the next function call, since it was | |||||
Lint: TODO Comment: This comment has a TODO. | |||||
// probably just a backward compatibility hack for the lack | |||||
// of utf8_decode() that - in any case - we should not use it. | |||||
// Note that - in any case - this replacement probably works since PHP 4: | |||||
// strlen( mb_convert_encoding($string, 'ISO-8859-1') ) | |||||
// So it's really probably the case to just drop this case. | |||||
return count(phutil_utf8v($string)); | return count(phutil_utf8v($string)); | ||||
} | } | ||||
/** | /** | ||||
* Find the console display length of a UTF-8 string. This may differ from the | * Find the console display length of a UTF-8 string. This may differ from the | ||||
* character length of the string if it contains double-width characters, like | * character length of the string if it contains double-width characters, like | ||||
* many Chinese characters. | * many Chinese characters. | ||||
▲ Show 20 Lines • Show All 685 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
This comment has a TODO.