I'm using the PHP iconv function to convert accented characters to their non-accented correspondents. For example, "ç" becomes "c", "é" becomes "e" and so on...
For example, this code converts the word "ação" to "acao":
<?php
$filename = "ação";
$filename = iconv( 'UTF-8', 'ASCII//TRANSLIT', $filename); // convert latin characters
echo $filename; // shows "acao"
Check the code above here:
However, running the same function inside my Wordpress site, it shows:
a??o
How to fix this?
I'm using iconv
inside a custom function of the plugin WP All Import:
I'm using the PHP iconv function to convert accented characters to their non-accented correspondents. For example, "ç" becomes "c", "é" becomes "e" and so on...
For example, this code converts the word "ação" to "acao":
<?php
$filename = "ação";
$filename = iconv( 'UTF-8', 'ASCII//TRANSLIT', $filename); // convert latin characters
echo $filename; // shows "acao"
Check the code above here: https://ideone/tv5C03
However, running the same function inside my Wordpress site, it shows:
a??o
How to fix this?
I'm using iconv
inside a custom function of the plugin WP All Import:
To solve this problem I need to insert this instruction BEFORE iconv
:
setlocale( LC_ALL, "pt_BR.UTF-8");
Thus, the conversion is done correctly (in my case, because my site is in pt_BR
):
<?php
$filename = "ação";
setlocale( LC_ALL, "pt_BR.UTF-8"); // fix the locale
$filename = iconv( 'UTF-8', 'ASCII//TRANSLIT', $filename); // convert latin characters
echo $filename; // shows "acao"
Correct result:
acao