views:

39

answers:

3
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){

   switch ($in) {
   case 'Á':
          return 'A';
   case 'á':
          return 'a';
   default:
         return $in;     
   }
}

This is the code. Doesn't work. Any thoughts? Oh yeah. It always enters the dafault exit for any input. perhaps it is somethig to do with how php handles chars X strings? I don't know.

+1  A: 

Instead of switching the character itself, switch the character code. It's dangerous to embed ASCII Extended characters directly in a string, raw. Sometimes even the editor you are using to write the code may save the characters incorrectly, if you have the wrong encoding specified.

SimpleCoder
Hm... Yeah perhaps, but it should work as it is, shouldn´t it?
Felipe Almeida
It's always a little dangerous to embed ASCII Extended characters raw in code like that.
SimpleCoder
+1  A: 

You should use str_replace instead:

$input = str_replace(array('Á', 'á'), array('A', 'a'), $input);

That does the exact same work as your switch statement.

halfdan
Not really - you meant: `$input = str_replace(array('Á', 'A'), array('á', 'a'), $input);` - and see dangers of embedding ASCII
Peter Ajtai
I'll try that. Thanks
Felipe Almeida
I agree with Peter; It doesn't really matter how you do the actual replacing. As long as you are embedding extended ASCII characters directly, there is the possibility for the code to fail.
SimpleCoder
A: 

Yeah. I changed the code slightly to this

        switch ($in) {
        case 'B':
            return 'A';
        case 'b':
            return 'a';
        default:
            return $in;     
    }

for testing purposes and it worked. Thanks to all.

Felipe Almeida