0

So I'm trying to convert latin characters such as á, é, etc. into their non-latin transliterations (a, e, etc.) I know there is the following: PHP replacing special characters like à->a, è->e

But none of them seem to have helped... Here is what I have and the results they produce.

echo $this->data['last_name'];
$last_name = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $this->data['last_name']);
echo $last_name;
$last_name = mb_convert_encoding($this->data['last_name'], 'ISO-8859-1');
echo $last_name;
$last_name = iconv('UTF-8', 'ascii//TRANSLIT//IGNORE', $this->data['last_name']);
echo $last_name;

-----

Dérmenjian
D�rmenjian
D�rmenjian
D?rmenjian

Any idea what I'm doing wrong and potentially how to fix it? I could always just do a massive array, but I'd prefer a programmatic approach to this problem...

(Not sure it makes a difference, but we're using php 5.5)

Community
  • 1
  • 1
Aram Papazian
  • 2,453
  • 6
  • 38
  • 45

3 Answers3

0

Turns out the solution was that the locale wasn't being set properly. According to: http://php.net/manual/en/function.iconv.php#74101 The default was C on my server, so by doing the following I got it to work:

setlocale(LC_ALL,'en_US.UTF8');

I had previously tried 'en_US' and that was failing.

Aram Papazian
  • 2,453
  • 6
  • 38
  • 45
0

Or you just use:

str_replace(array("á", "é"), array("a", "e"), $subject);
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Genmais
  • 1,099
  • 2
  • 9
  • 14
  • I think you had missed the part where I said programmatically. I can easily do one massive array, but then I'd have to sit there and take every single non-ascii character and make some massive array. Not only is that going to be a headache (how do I know I didn't miss one?) but also since it's a human doing it, extremely prone to error (what if I do it slightly wrong?). Hence why I asked programmatically. =) – Aram Papazian Feb 24 '15 at 07:41
0

You can use "UTF8::str_transliterate" -> you can install it via composer -> https://packagist.org/packages/voku/portable-utf8

e.g.: UTF8::str_transliterate('déjà σσς iıii') // deja sss iiii

... or you can use -> https://packagist.org/packages/voku/urlify also available via composer, here you can prefer one language.

e.g.: URLify::filter('Cağaloğlu, çalıştığı, müjde, lazım, mahkûm', 60, 'tr'); // "Cagaloglu-calistigi-mujde-lazim-mahkum"

Mfg Lars :)

Lars Moelleken
  • 719
  • 8
  • 17