PHP: Convert specific-Bosnian characters to non-bosnian (utf8 standard chars)

浪子不回头ぞ 提交于 2019-12-11 02:05:23

问题


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!


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8386746/php-convert-specific-bosnian-characters-to-non-bosnian-utf8-standard-chars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!