BobGCA2
BobGCA2

Reputation: 125

Remove accents - Replace accented letters by letters without accents with str_replace

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.

Upvotes: 0

Views: 1903

Answers (2)

Vincent Savard
Vincent Savard

Reputation: 35927

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.

Upvotes: 1

deceze
deceze

Reputation: 522372

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.

Upvotes: 0

Related Questions