PHP `iconv` not working inside a Wordpress site

admin2025-04-21  0

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?


Edit

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?


Edit

I'm using iconv inside a custom function of the plugin WP All Import:

Share Improve this question edited Sep 7, 2019 at 2:21 Rogério Dec asked Sep 7, 2019 at 2:00 Rogério DecRogério Dec 1993 silver badges12 bronze badges 3
  • Where and how are you using it? – Jacob Peattie Commented Sep 7, 2019 at 2:04
  • I'm using it inside a custom function of the plugin wordpress/plugins/wp-all-import – Rogério Dec Commented Sep 7, 2019 at 2:11
  • Found a solution – Rogério Dec Commented Sep 7, 2019 at 13:17
Add a comment  | 

1 Answer 1

Reset to default 0

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745177993a289017.html

最新回复(0)