5

I'm looking for way to convert chars like āžšķūņrūķīš to azskunrukis. In other words, to replace ā with a, ž with z and so. Is there anything built-in, or I should create my own "library" of from-to symbols?

Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
daGrevis
  • 21,014
  • 37
  • 100
  • 139
  • 1
    What is the purpose of this conversion? If you want to make URLs out of that I'd rather recommend you to get UTF-8 encoded URLs right as gooogle will like them much more than crippled text in non english languages. Most modern browsers will also show them correctly in the address bar. – x4u Jun 01 '11 at 18:54
  • possible duplicate of [How to handle diacritics (accents) when rewriting 'pretty URLs'](http://stackoverflow.com/questions/465990/how-to-handle-diacritics-accents-when-rewriting-pretty-urls) – joelhardi Jun 01 '11 at 19:05
  • Just keep in mind that e.g., ü and u are _very_ different letters. – Michael Stum Jun 01 '11 at 22:37
  • Check this, it worked for me : https://stackoverflow.com/a/54257810/10159774 – Lu Blue Jan 18 '19 at 16:25

3 Answers3

6

Take a look at iconv's transliteration capabilities:

<?php
$text = "This is the Euro symbol '€'.";

echo 'Original : ', $text, PHP_EOL;
echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;
echo 'IGNORE   : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL;
echo 'Plain    : ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;

?>

The above example will output something similar to:

Original : This is the Euro symbol '€'.
TRANSLIT : This is the Euro symbol 'EUR'.
IGNORE : This is the Euro symbol ''.
Plain :
Notice: iconv(): Detected an illegal character in input string in .\iconv-example.php on line 7
This is the Euro symbol '

Your example text can be tranliterated using:

$translit = iconv('UTF-8', 'US-ASCII//TRANSLIT', 'āžšķūņrūķīš');

Here's an example with the text you provided: http://ideone.com/MJHvf

  • 1
    I think he wants `iconv('UTF-8', 'US-ASCII//TRANSLIT', 'āžšķūņrūķīš');` rather than to convert to ISO-8859-1, but otherwise this is the answer. Here's a [non-iconv](http://stackoverflow.com/questions/3542818/remove-accents-without-using-iconv) answer for people without it. – joelhardi Jun 01 '11 at 19:08
  • Sweet, we wouldn't want this guy to get only a B on his homework. :) – joelhardi Jun 01 '11 at 20:29
1

I'm not sure of any functions that do this directly, but there are some implementations of translation tables that do something like that in the comments on strtr's documentation page. They end up using a table that directly translates each character to its equivalent, i.e. "ž" => "z".

thetaiko
  • 7,816
  • 2
  • 33
  • 49
1

As an alternative to iconv, you could check out the Normalize functions of the intl extension (if available).

Nev Stokes
  • 9,051
  • 5
  • 42
  • 44