在多种语言中使用 substr(),包括阿拉伯语和 php

发布于 2024-11-17 04:38:29 字数 381 浏览 2 评论 0原文

我知道在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有深☉意 2024-11-24 04:38:29

对于阿拉伯语单词:
我只想让角色处于位置 2。

$name = "MYNAME";
$Char = substr($Name,2,2);
echo $Char; // print N this okay.

但对于像 كתח 这样的阿拉伯语工作,它会返回问号

case 1: $Char = substr($Name,0,1);     // Not Work and return question mark
case 2: $Char = substr($Name,1,1);     // Not Work and return question mark
case 3: $Char = substr($Name,0*2,1*2); // Work and return "ك"
case 3: $Char = substr($Name,1*2,1*2); // Work and return "ا"

所以我找到了解决方案:

$Char = mb_substr($Name,1,1,'utf-8');  // Work and return "ا".

For Arabic word:
I want get the character in position 2 only.

$name = "MYNAME";
$Char = substr($Name,2,2);
echo $Char; // print N this okay.

But for Arabic work like كامل, it returns question mark

case 1: $Char = substr($Name,0,1);     // Not Work and return question mark
case 2: $Char = substr($Name,1,1);     // Not Work and return question mark
case 3: $Char = substr($Name,0*2,1*2); // Work and return "ك"
case 3: $Char = substr($Name,1*2,1*2); // Work and return "ا"

So I find the solution:

$Char = mb_substr($Name,1,1,'utf-8');  // Work and return "ا".
御守 2024-11-24 04:38:29

首先,忘记substr。如果您打算使用 UTF-8 编码字符串并将其拆分,那么 mb_substr 是唯一可行的解​​决方案。

您还需要确保MySql的连接编码也是UTF-8。 调用来完成此操作

mysql_set_charset('utf8');

通过在 mysql_connect 之后 。如果您使用 mysql 扩展之外的数据访问层(例如 PDO),则可以使用等效的方法来执行此操作。

First of all, forget substr. If you are going to encode your strings in UTF-8 and splitting them up, then mb_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

mysql_set_charset('utf8');

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).

木格 2024-11-24 04:38:29
  1. 检查您是否正在使用其他多字节 (mb_*) 函数来处理数据。
  2. 检查您的脚本是否保存为 UTF-8 文本文件。
  3. 检查连接到数据库后是否立即向数据库发送 SET NAMES utf8 查询。
  1. Check if you're using other multibyte (mb_*) functions for working with your data.
  2. Check if your scripts are saved as UTF-8 text files.
  3. Check if you send SET NAMES utf8 query to the database right after you connect to it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文