Home > Technology > Php substr截取中文乱码

Php substr截取中文乱码

Substr是php的一个内置函数.要求php4.函数作用官方描述为substr — Return part of a string.即截取字符串.但是在截取的中文时候有时可能存在乱码.今天在弄主题head优化的时候遇到了这个问题.
原因:

  1. 对于英文来说是一个字母一个字节(8位)来编码,而中文却是一个汉字三个字节(24位)来编码.
  2. Substr不能很好区分全角半角.尤其是在中文环境下.

网上可以查到许多的解决方案.但是最简单的是用mb_substr替换.但前提是你的php版本需4.0.6或更高.
使用方法:

mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )
Header.php的优化主要参照水煮鱼的方法,打算下一篇日志写. :grin:

Ps.但愿解决了IE下查看源文件乱码的问题.哈哈大家帮我看看.

  1. Zoll Nov 27th, 2008 @ 22:17 | #1

    哎呀 我也是正苦恼这个问题呢 也去试试 :razz:

  2. 老时 Nov 27th, 2008 @ 22:35 | #2

    老时来膜拜鸟

  3. yinheli Nov 27th, 2008 @ 22:45 | #3

    @老时
    恭候老时大人……老时的话都是精品啊

  4. DNF1100 Nov 4th, 2009 @ 10:51 | #4

    加到哪里?

  5. DNF1100 Nov 4th, 2009 @ 10:58 | #5

    哈哈,解决了,谢谢博主。

Submitting Comment, Give me a second...

Leave a comment

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackbacks & Pingbacks ( 1 )
  1. Nov 30th, 2008 @ 00:20 | #1
    Trackback: PhilNa