【问题标题】:Replace unicode with actual data in php用php中的实际数据替换unicode
【发布时间】:2015-10-07 10:31:33
【问题描述】:

我想用 php 中的实际数据替换 unicode \u00a0

例如:"<b>Sort By\u00a0-\u00a0</b><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/atoz/1.html">A to Z\u00a0|\u00a0</a><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/recent/1.html">Recently Added\u00a0|\u00a0</a><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/downloads/1.html">Most Downloaded</a>"

【问题讨论】:

  • "Sort By\u00a0" → 你是什么意思,(1)字符串包含序列\u00a0(6个字符),或者(2)字符串包含不间断空格(1个字符)?

标签: php html unicode


【解决方案1】:

\u00a0escape sequenceescape sequence

要解码PHP中的any escape sequence,您可以使用此函数:

function unicodeString($str, $encoding=null) {
    if (is_null($encoding)) $encoding = ini_get('mbstring.internal_encoding');
    return preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/u', create_function('$match', 'return mb_convert_encoding(pack("H*", $match[1]), '.var_export($encoding, true).', "UTF-16BE");'), $str);
}
echo unicodeString($str);
//<b>Sort By - </b><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/atoz/1.html">A to Z | </a><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/recent/1.html">Recently Added | </a><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/downloads/1.html">Most Downloaded</a>

演示:

https://ideone.com/9QtzMO


如果您只需要替换单个 escape sequence,请使用:

$str = str_replace("\u00a0", " ", $str);
echo $str;
<b>Sort By - </b><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/atoz/1.html">A to Z | </a><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/recent/1.html">Recently Added | </a><a href="http://pagalworld.com/files/9704/Dil Dhadakne Do (2015) Mp3 Songs/downloads/1.html">Most Downloaded</a>

【讨论】:

  • 请注意,第一个函数不会替换单个 unicode 字符,而是替换转义序列。
  • 感谢您的功能!!救了我的命!
  • 我发现这只替换了第一个找到的表情符号。如何替换字符串中的所有表情符号?它也不会取代一些表情符号,例如 \ud83d\udc9 (?)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多