13

エンコードされた長いダッシュを数値エンティティから文字列にデコードしようとしていますが、これを適切に実行できる関数が見つからないようです。

私が見つけた最高のものは mb_decode_numericentity() ですが、何らかの理由で長いダッシュやその他の特殊文字をデコードできません。

$str = '–';

$str = mb_decode_numericentity($str, array(0xFF, 0x2FFFF, 0, 0xFFFF), 'ISO-8859-1');

これは「?」を返します。

誰でもこの問題を解決する方法を知っていますか?

4

2 に答える 2

19

次のコード スニペット (ほとんどがここから盗用され、改良されています) は、リテラル、10 進数、および 16 進数のエンティティに対して機能します。

header("content-type: text/html; charset=utf-8");

/**
* Decodes all HTML entities, including numeric and hexadecimal ones.
* 
* @param mixed $string
* @return string decoded HTML
*/

function html_entity_decode_numeric($string, $quote_style = ENT_COMPAT, $charset = "utf-8")
{
$string = html_entity_decode($string, $quote_style, $charset);
$string = preg_replace_callback('~&#x([0-9a-fA-F]+);~i', "chr_utf8_callback", $string);
$string = preg_replace('~&#([0-9]+);~e', 'chr_utf8("\\1")', $string);
return $string; 
}

/** 
 * Callback helper 
 */

function chr_utf8_callback($matches)
 { 
  return chr_utf8(hexdec($matches[1])); 
 }

/**
* Multi-byte chr(): Will turn a numeric argument into a UTF-8 string.
* 
* @param mixed $num
* @return string
*/

function chr_utf8($num)
{
if ($num < 128) return chr($num);
if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
return '';
}


$string ="&#x201D;"; 

echo html_entity_decode_numeric($string);

改善提案は大歓迎です。

于 2011-06-05T13:49:58.530 に答える
1

mb_decode_numericentity16 進数を処理せず、10 進数のみを処理します。次の方法で期待どおりの結果が得られますか?

$str = '–';

$str = mb_decode_numericentity ( $str , Array(255, 3145727, 0, 65535) , 'ISO-8859-1');

を使用hexdecして、16 進数を 10 進数に変換できます。

また、好奇心から、次の作業を行います。

$str = '&#8211;';

 $str = html_entity_decode($str);
于 2010-05-04T11:31:49.783 に答える