mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-28 23:03:25 +09:00
Encode non-BMP UTF-8 characters as HTML entities
This commit is contained in:
parent
7d44db1dcb
commit
d474c20a36
4 changed files with 31 additions and 2 deletions
|
|
@ -5,3 +5,19 @@
|
|||
*
|
||||
* Copyright (c) Rhymix Developers and Contributors
|
||||
*/
|
||||
|
||||
/**
|
||||
* Encode UTF-8 characters outside of the Basic Multilingual Plane in the &#xxxxxx format.
|
||||
* This allows emoticons and other characters to be stored in MySQL without utf8mb4 support.
|
||||
*
|
||||
* @param $str The string to encode
|
||||
* @return string
|
||||
*/
|
||||
function utf8_mbencode($str)
|
||||
{
|
||||
return preg_replace_callback('/[\xF0-\xF7][\x80-\xBF]{3}/', function($m) {
|
||||
$bytes = array(ord($m[0][0]), ord($m[0][1]), ord($m[0][2]), ord($m[0][3]));
|
||||
$codepoint = ((0x07 & $bytes[0]) << 18) + ((0x3F & $bytes[1]) << 12) + ((0x3F & $bytes[2]) << 6) + (0x3F & $bytes[3]);
|
||||
return '&#x' . dechex($codepoint) . ';';
|
||||
}, $str);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue