Changeset View
Changeset View
Standalone View
Standalone View
src/utils/utf8.php
Show First 20 Lines • Show All 282 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('mb_strlen')) { | ||||
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. | |||||
// https://we.phorge.it/T15188 | |||||
return mb_strlen($string, 'UTF-8'); | |||||
} | } | ||||
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 | ||||
▲ Show 20 Lines • Show All 686 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