关于 PHP 中的扩展 ASCII/编码的帮助!

发布于 2024-09-24 07:07:15 字数 824 浏览 1 评论 0原文

各位晚上好。

这是我的代码:

static private  function removeAccentedLetters($input){
    for ($i = 0; $i < strlen($input); $i++) {
        $input[$i]=self::simplify($input[$i]);
    }
    return $input;
}
static private function simplify($in){
    $ord=ord($in);
    switch ($ord) {
        case 193: //Á...
        return 'A';
        case 98: //b
        return 'a';
        default:
        return $in;
    }
}

好的。这是不起作用的部分

case 193: //Á...
  return 'A';

这是起作用的部分:

case 98: //b
return 'a';

这些仅用于测试目的。

谁能告诉我发生了什么事吗?我之前也遇到过同样的错误,但现在我在代码本身中没有使用任何扩展 ASCII,这是之前错误的原因。

我认为这一定与字符编码有关,但我不确定。顺便说一句,我在 Eclipse 中编码,根据它,我使用的字符编码是 Cp1252。

哦,是的,代码应该消除任何重音字母,例如 á à 并用基本的 vogals 替换它们,即 á->a

提前致谢

Good Evening folks.

This is my code:

static private  function removeAccentedLetters($input){
    for ($i = 0; $i < strlen($input); $i++) {
        $input[$i]=self::simplify($input[$i]);
    }
    return $input;
}
static private function simplify($in){
    $ord=ord($in);
    switch ($ord) {
        case 193: //Á...
        return 'A';
        case 98: //b
        return 'a';
        default:
        return $in;
    }
}

Ok. This is the bit that doesn't work

case 193: //Á...
  return 'A';

And this is the bit that does:

case 98: //b
return 'a';

These are just for testing purposes.

Could anyone tell me what's happening? I had the same sort of error before but now I'm not using any extended ASCII in the code itself, which was the cause of error before.

I'm thinking it must have something to do with character encoding but I'm not sure. By the way, I'm coding in Eclipse and, according to it, the character encoding I'm using is Cp1252.

Oh, and yes, the code is supposed to eliminate any accented Letters such as á à and replace them with their basic vogals, i.e. á->a

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

吃颗糖壮壮胆 2024-10-01 07:07:15

如果您有多字节字符,并且您正在使用 strlen() 循环遍历每个字符来检查是否已循环,是否可能? strlen() 假定 1 个字节 == 1 个字符。

我会研究 PHP 的现有音译库

Could it be that if you have multi byte characters, and you are looping through each character using strlen() to check if you have looped through? strlen() assumes 1 byte == 1 character.

I'd look into existing transliteration libraries for PHP.

舂唻埖巳落 2024-10-01 07:07:15

也许这个函数与 mb_strlen 结合使用可以帮助您:

mb_strcut
或者
mb_substr

编辑:例如你可以这样

$string = 'cioèòà';
for ($i=0;$i<mb_strlen($string);$i++) {
  echo mb_substr($string, $i, 1);
}

:会回显所有单个字符。

Maybe this function helps you in combination with mb_strlen:

mb_strcut
or
mb_substr

EDIT: For example you could go like this:

$string = 'cioèòà';
for ($i=0;$i<mb_strlen($string);$i++) {
  echo mb_substr($string, $i, 1);
}

This would echo you all the single chars out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文