在多种语言中使用 substr(),包括阿拉伯语和 php
我知道在 php 中已经用阿拉伯语发布了很多问题,但我无法解决我的问题,因此我发布这个问题:
我有一个以英语完美运行的 PhP 网站。现在,我希望它支持多种语言,包括法语、西班牙语、阿拉伯语,但我无法通过一个代码使用它们。问题是,我在很多地方使用了 substr() ,但翻译后的字符不能按 substr() 的预期工作。我也尝试过使用 mb_substsr(),但没有用:(
DB 中的字段是“utf8_general_ci”,我已经在我的代码中放置了 header('Content-type: text/html; charset=UTF-8');允许以 UTF-8 渲染
问题是我得到“??????”代替了确切的单词,或者我使用 substr() 得到了错误的单词
请帮助!
I know there are a lot of questions already posted with arabic language in php, but I was unable to get solution to my problem and hence I am posting this question:
I have a PhP site that is running perfectly in English language. Now, I want it to support multiple languages including French, Spanish, Arabic but I am unable to use them with one code. The problem is, I have used substr() in many places and the translated characters not work as intended with substr(). I also tried with mb_substsr(), but of no use :(
The field in DB is "utf8_general_ci" and I have already placed header('Content-type: text/html; charset=UTF-8'); in my code to allow rendering in UTF-8.
The problem is either I get "?????" in place of the exact words or I get incorrect words with substr()
Please help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于阿拉伯语单词:
我只想让角色处于位置 2。
但对于像
كתח
这样的阿拉伯语工作,它会返回问号所以我找到了解决方案:
For Arabic word:
I want get the character in position 2 only.
But for Arabic work like
كامل
, it returns question markSo I find the solution:
首先,忘记
substr
。如果您打算使用 UTF-8 编码字符串并将其拆分,那么mb_substr
是唯一可行的解决方案。您还需要确保MySql的连接编码也是UTF-8。 调用来完成此操作
通过在
mysql_connect
之后 。如果您使用 mysql 扩展之外的数据访问层(例如 PDO),则可以使用等效的方法来执行此操作。First of all, forget
substr
. If you are going to encode your strings in UTF-8 and splitting them up, thenmb_substr
is the only working solution.You also need to make sure that the connection encoding of MySql is also UTF-8. Do this by calling
just after
mysql_connect
. There are equivalent ways to do this if you are using a data access layer other than the mysql extension (such as PDO).SET NAMES utf8
查询。SET NAMES utf8
query to the database right after you connect to it.