(鉑) string functions and UTF8 in php

痞子三分冷 提交于 2019-12-05 09:35:54

Make sure you set the proper internal encoding:

<?php
echo mb_internal_encoding() . '<br />';

echo mb_strlen('鉑', 'utf-8') . '<br />';
echo mb_strlen('鉑') . '<br />';

mb_internal_encoding('utf-8');
echo mb_internal_encoding() . '<br />';
echo mb_strlen('鉑') . '<br />';

// ISO-8859-1
// 1
// 3
// UTF-8
// 1

You will likeliy need to add the character set:

  echo mb_strlen("鉑","utf-8");

Set the encoding to your mb_strlen function:

echo mb_strlen("鉑", "UTF-8");

If you do the following, you will get the correct answer

echo mb_strlen("鉑", "UTF-8");

I'm guess php is defaulting to ASCII which produces an answer of 3. I also found a very interesting article on Encoding for anyone interested in why and how it works. http://www.joelonsoftware.com/articles/Unicode.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!