0

str_replace does not replace accented letters by letters without accent. What's wrong with that?

This returns the expected result:

<?php
    $string = get_post_custom_values ("text");
    // Say get_post_custom_values ​​("text") equals "José José"
    $string = str_replace(" ", "-", $string);

    echo $string [0];
    // Output "José-José"
?>

This does not work:

<?php
    $string = get_post_custom_values ("text");
    // Say get_post_custom_values ​​("text") equals "Joseph Joseph"
    $string = str_replace("é", "e", $string);

    echo $string [0];
    // Output "José José". Nothing has changed
?>

Note: Translated from the Portuguese language with GoogleTranslate.

Alex Korban
  • 14,916
  • 5
  • 44
  • 55
BobGCA2
  • 125
  • 10
  • See http://stackoverflow.com/questions/1728746/how-to-properly-handle-international-character-in-php-mysql-apache, "PHP" section. – ivan_pozdeev Mar 04 '12 at 03:33

2 Answers2

1

The easy, safe way to remove every accented letters is by using iconv :

setlocale(LC_ALL, "fr_CA.utf8"); // for instance
$output = iconv("utf-8", "ascii//TRANSLIT", $input);

Your current problem is most likely caused by a different encoding.

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
0

The character é as saved in your source code is not in the same encoding as the data you get back from get_post_custom_values. Encoding doesn't match → not recognized as the same character → not replaced.

deceze
  • 510,633
  • 85
  • 743
  • 889