문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top