PHP strlen和mb_strlen无法正常工作 [英] PHP strlen and mb_strlen not working as expected

查看:67
本文介绍了PHP strlen和mb_strlen无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在字符串上运行它们时,PHP函数strlen()和mb_strlen()都返回错误的字符数。

PHP functions strlen() and mb_strlen() both are returning the wrong number of characters when I run them on a string.

这是一段代码我正在使用...

Here is a piece of the code I'm using...

 $foo = mb_strlen($itemDetails['ITEMDESC'], 'UTF-8');
 echo $foo;

这是告诉我这种刺痛-4½直虹膜剪刀长45个字符,它是27 。

It is telling me this sting - "4½" Straight Iris Scissors" is 45 characters long. It's 27.

它还告诉我,此字符串-婴儿脚跟保温器,无粘合剂垫,100 / cs是54,是正确的。

It also tells me that this string - "Infant Heel Warmer, No Adhesive Attachment Pad, 100/cs" is 54, which is correct.

我认为它与字符编码有关,我认为一切都应该是UTF-8。我尝试给mb_strlen()提供几种不同的字符编码类型,它们都返回了带有具有这些非标准字符的字符串。

I assume its some issue with character encoding, everything should be UTF-8 I think. I've tried feeding mb_strlen() several different character encoding types and they all are returning this oddball count with the string that has those non-standard characters.

我不知道为什么会这样。

I've no idea why this is happening.

推荐答案

仔细检查您的文本是否真的是UTF-8。Â字符对我来说就像是经典的字符编码问题。您应该检查从原点开始的整个路径通过上面引用的代码中的点,可以看到大量文本,因为在很多地方都可能混用编码。

Double-check whether your text really is UTF-8 or not. That "Â" character makes it look like a classic character encoding problem to me. You should check the entire path from the origin of the text through the point in your code that you quoted above, because there are a lot of places where the encodings can get munged.

文本原点是从HTML表单吃了?确保您的< form> 元素包含 accept-charset = UTF-8 属性。

Did the text originate from an HTML form? Ensure your <form> element includes the accept-charset="UTF-8" attribute.

文本是否一直存储在数据库中?确保数据库以UTF-8存储并返回数据。这意味着检查服务器的全局默认值,数据库或架构的默认值以及表本身。

Did the text get stored in a database along the way? Make sure the database stores and returns the data in UTF-8. This means checking the server's global defaults, the defaults for the database or schema, and the table itself.

这篇关于PHP strlen和mb_strlen无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆