Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

php - Change foreign characters to their roman equivalent

I am using php and I was wondering if there was a predefined way to convert foreign characters to their non-foreign alternatives.

Characters such as ê, ?, é all resulting to 'e'.
I'm looking for a function that would take a string and return it without the special characters.
Any ideas would be greatly appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After failing to find suitable convertors I created my own collection that suits my needs including my favorite Cyrillic conversion that by default has numerous variations.

function transliterateString($txt) {
    $transliterationTable = array('á' => 'a', 'á' => 'A', 'à' => 'a', 'à' => 'A', '?' => 'a', '?' => 'A', 'a' => 'a', '?' => 'A', '?' => 'a', '?' => 'A', '?' => 'a', '?' => 'A', '?' => 'a', '?' => 'A', 'ā' => 'a', 'ā' => 'A', '?' => 'ae', '?' => 'AE', '?' => 'ae', '?' => 'AE', '?' => 'b', '?' => 'B', '?' => 'c', '?' => 'C', '?' => 'c', '?' => 'C', '?' => 'c', '?' => 'C', '?' => 'c', '?' => 'C', '?' => 'c', '?' => 'C', '?' => 'd', '?' => 'D', '?' => 'd', '?' => 'D', '?' => 'd', '?' => 'D', 'e' => 'dh', 'D' => 'Dh', 'é' => 'e', 'é' => 'E', 'è' => 'e', 'è' => 'E', '?' => 'e', '?' => 'E', 'ê' => 'e', 'ê' => 'E', 'ě' => 'e', 'ě' => 'E', '?' => 'e', '?' => 'E', '?' => 'e', '?' => 'E', '?' => 'e', '?' => 'E', 'ē' => 'e', 'ē' => 'E', '?' => 'f', '?' => 'F', '?' => 'f', '?' => 'F', '?' => 'g', '?' => 'G', '?' => 'g', '?' => 'G', '?' => 'g', '?' => 'G', '?' => 'g', '?' => 'G', '?' => 'h', '?' => 'H', '?' => 'h', '?' => 'H', 'í' => 'i', 'í' => 'I', 'ì' => 'i', 'ì' => 'I', '?' => 'i', '?' => 'I', '?' => 'i', '?' => 'I', '?' => 'i', '?' => 'I', '?' => 'i', '?' => 'I', 'ī' => 'i', 'ī' => 'I', '?' => 'j', '?' => 'J', '?' => 'k', '?' => 'K', '?' => 'l', '?' => 'L', '?' => 'l', '?' => 'L', '?' => 'l', '?' => 'L', '?' => 'l', '?' => 'L', '?' => 'm', '?' => 'M', 'ń' => 'n', '?' => 'N', 'ň' => 'n', '?' => 'N', '?' => 'n', '?' => 'N', '?' => 'n', '?' => 'N', 'ó' => 'o', 'ó' => 'O', 'ò' => 'o', 'ò' => 'O', '?' => 'o', '?' => 'O', '?' => 'o', '?' => 'O', '?' => 'o', '?' => 'O', '?' => 'oe', '?' => 'OE', 'ō' => 'o', 'ō' => 'O', '?' => 'o', '?' => 'O', '?' => 'oe', '?' => 'OE', '?' => 'p', '?' => 'P', '?' => 'r', '?' => 'R', '?' => 'r', '?' => 'R', '?' => 'r', '?' => 'R', '?' => 's', '?' => 'S', '?' => 's', '?' => 'S', '?' => 's', '?' => 'S', '?' => 's', '?' => 'S', '?' => 's', '?' => 'S', '?' => 's', '?' => 'S', '?' => 'SS', '?' => 't', '?' => 'T', '?' => 't', '?' => 'T', '?' => 't', '?' => 'T', '?' => 't', '?' => 'T', '?' => 't', '?' => 'T', 'ú' => 'u', 'ú' => 'U', 'ù' => 'u', 'ù' => 'U', '?' => 'u', '?' => 'U', '?' => 'u', '?' => 'U', '?' => 'u', '?' => 'U', '?' => 'u', '?' => 'U', '?' => 'u', '?' => 'U', '?' => 'u', '?' => 'U', 'ū' => 'u', 'ū' => 'U', '?' => 'u', '?' => 'U', 'ü' => 'ue', 'ü' => 'UE', '?' => 'w', '?' => 'W', '?' => 'w', '?' => 'W', '?' => 'w', '?' => 'W', '?' => 'w', '?' => 'W', 'y' => 'y', 'Y' => 'Y', '?' => 'y', '?' => 'Y', '?' => 'y', '?' => 'Y', '?' => 'y', '?' => 'Y', '?' => 'z', '?' => 'Z', '?' => 'z', '?' => 'Z', '?' => 'z', '?' => 'Z', 't' => 'th', 'T' => 'Th', 'μ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
    return str_replace(array_keys($transliterationTable), array_values($transliterationTable), $txt);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...