PHP strlen和mb

编程入门 行业动态 更新时间:2024-10-23 17:28:29
本文介绍了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

本文发布于:2023-11-29 23:12:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647917.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   strlen   mb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!