将古怪的字符转换为UTF-8代码引用


Converting wacky chars to UTF-8 code reference

我使用的是pbkdf2(),我需要打印它的输出来调试错误。它会生成奇怪的字符串,如"O" BIa"O"!J"。

htmlentities()或htmlSpecialChars()都不能将这些字符转换为更可读的内容。

我正在考虑将它们转换为UTF-8实体,如激,看看浏览器是否会以这种方式更好地呈现它们。如果没有,我宁愿看到他们的UTF-8号而不是这些字符。

我尝试了utf8_encode()和utf8_decode(),单独并与htmlentities()和htmlSpecialChars()相结合。一点也不走运。

你知道该怎么做吗?

试试这个函数:

function convert_smart_quotes($string)
{
$string = htmlentities($string);
$string = mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8');
$string = htmlspecialchars_decode(utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8', false)));
$s = array(
    chr(145) => "'",
    chr(146) => "'",
    chr(147) => '"',
    chr(148) => '"',
    chr(151) => '-',
    's©' => '©',
    '®' => '®',
    '™' => '™', //™
    '“' => '"', // left side double smart quote
    'â€' => '"', // right side double smart quote
    '‘' => "'", // left side single smart quote
    '’' => "'", // right side single smart quote
    '…' => '...', // elipsis
    '—' => '-', // em dash
    '–' => '-', // en dash
);
return strtr($string, $s);
}