关于 PHP 中的扩展 ASCII/编码的帮助!
各位晚上好。
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有多字节字符,并且您正在使用
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.
也许这个函数与 mb_strlen 结合使用可以帮助您:
mb_strcut
或者
mb_substr
编辑:例如你可以这样
:会回显所有单个字符。
Maybe this function helps you in combination with mb_strlen:
mb_strcut
or
mb_substr
EDIT: For example you could go like this:
This would echo you all the single chars out.