Nasıl JavaScript kaçış dizileri PHP oluşturabilirim?

2 Cevap php

PHP içinde (vekil çiftleri dahil) geçerli bir UTF-16 JavaScript kaçış dizisi karakterleri oluşturmak için bir yol arıyorum.

I'm using the code below to get the UTF-32 code points (from a UTF-8 encoded character). This works as JavaScript escape characters (eg. '\u00E1' for 'á') - until you get into the upper ranges where you get surrogate pairs (eg '

2 Cevap

Nasıl iconv (veya benzer mb_convert_encoding) kullanmaya ne dersiniz?

örn. gibi bir şey:

$utf16= iconv('UTF-8', 'UTF-16LE', $text);
$codeunits= array();
for ($i= 0; $i<strlen($utf16); $i+= 2) {
    $codeunits[]= ord($utf16{$i})+ord($utf16{$i+1})<<8;
}

İşte (bobince gelen cevaba göre) kullanılan son kodu bulunuyor:

$output = '';
$utf16 = iconv(fxCHARSET, 'UTF-16BE', $text);
for ($i= 0; $i < strlen($utf16); $i+= 2)
{
    $output.= '\\u'.str_pad(dechex((ord($character{$i}) << 8) + ord($character{$i+1})), 4, '0', STR_PAD_LEFT);
}