check if is multibyte string in PHP

后端 未结 3 686
庸人自扰
庸人自扰 2020-12-30 12:23

I want to check if is a string type multibyte on PHP. Have any idea how to accomplish this?

Example:



        
3条回答
  •  遥遥无期
    2020-12-30 13:00

    To determine if something is multibyte or not you need to be specific about which character set you're using. If your character set is Latin1, for example, no strings will be multibyte. If your character set is UTF-16, every string is multibyte.

    That said, if you only care about a specific character set, say utf-8, you can use a mb_strlen < strlen test if you specify the encoding parameter explicitly.

    function is_multibyte($s) {
      return mb_strlen($s,'utf-8') < strlen($s);
    }
    

提交回复
热议问题