Having problems with iconv not transliterating any more

35 views Asked by At

This code used to work and transliterate accented characters to 7-bit ASCII:

    setlocale(LC_CTYPE, 'en_US_POSIX.UTF-8');
    echo iconv('UTF-8', 'US-ASCII//TRANSLIT', 'Jalapeño Café');

But instead of Jalapeno Cafe, it's now outputting Jalapeo Caf.

I can see en_US_POSIX in the list of locales on the server.

The PHP version is 7.4.10.

The problem occurred before and after much experimentation I was able to find the locale and iconv parameters to fix it, but now it's not working. Any recommendations on what to do next?

1

There are 1 answers

2
Chris Haas On BEST ANSWER

When I inspect the return value of setlocale(LC_CTYPE, 'en_US_POSIX.UTF-8') I get false, which means there was an error. So if that appeared to work in the past, I'm betting there was something else kicking in before it.

You should be able to just use en_US for the locale.

if(false === setlocale(LC_CTYPE, 'en_US')){
    throw new \RuntimeException('Could not change the locale');
}

echo iconv('UTF-8', 'ASCII//TRANSLIT', 'Jalapeño Café');

// Jalapeno Cafe

Demo: https://3v4l.org/AVQhU