2

In Bosnia we have following characters only used in latin-form in Bosnia and Croatia, so I'd need to convert these letters as following:

FROM | TO
  ć  | c
  č  | c
  ž  | z
  š  | s
  đ  | dj

If this is possible with some special form of RegEx, or utf8_encode/decode, that informatiion and an appopriate example will be quite welcome! Thanks all.

PS - Want to achive this in PHP!

  • 2
    For the `đ` to `dj` conversion, you would need a regex or string replace. Everything else is already covered by [the many iconv](http://stackoverflow.com/questions/158241/php-replace-umlauts-with-closest-7-bit-ascii-equivalent-in-an-utf-8-string) answers. – mario Dec 05 '11 at 14:32

2 Answers2

4

You can try this:

$search = array("ć", "č", "ž", "š", "đ");
$replacement = array("c", "c", "z", "s", "dj");
$new_string = str_replace($search, $replacement, $string);

Also, check out str_replace

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
2

You can use this with iconv.

$result = iconv("UTF-8", "ASCII//TRANSLIT", $text);

That will work assuming your input $text is in utf-8. If it's in latin-1 then use

iconv("ISO-8859-1", "ASCII//TRANSLIT", $text);

Of cause your PHP must have iconv extension, most often iconv is enabled in php.ini file, but not always.

Dmitri Snytkine
  • 1,096
  • 1
  • 8
  • 14
  • 1
    What this does is translate non-ASCII char to the most similar replacement char in ASCII. For this to work you should also set your locale before calling this function: setlocale(LC_ALL, 'en_US'); – Dmitri Snytkine Dec 05 '11 at 14:34