duangan6797 2013-11-26 10:12
浏览 58
已采纳

substr()外来字符给我带有白色问号的黑色钻石

I'm taking foreign (Japanese) characters from a database and using substr() to limit the length of the string.

However when I do this it cuts off a character from the string and that leaves behind one of those question marks in black diamonds as a replacement character (�)

Everything (Documents, Charset, table encoding) are set to UTF-8.

Here is an example of what happens

$string = "日本最大級のポータルサイト。"
echo substr($string, 0,10); 

Which outputs 日本最�

How do you reccomend I find/replace this question mark icon?

  • 写回答

2条回答 默认 最新

  • dongye9071 2013-11-26 10:15
    关注

    You can not use substr() when dealing with UTF-strigs since each symbol there will be represented as multiple bytes, not single byte (for non-ASCII characters). And substr() works with bytes. Instead you should use mb_substr() which will safely and correct return desired result.

    To work with multibyte strings in PHP there is mbstring extension, and mb_substr() is part of it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿