如何使用 PHP 检查单词是日语还是英语
我想在此功能中对英语单词和日语单词进行不同的处理,
function process_word($word) {
if($word is english) {
/////////
}else if($word is japanese) {
////////
}
}
谢谢
I want to have different process for English word and Japanese word in this function
function process_word($word) {
if($word is english) {
/////////
}else if($word is japanese) {
////////
}
}
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不需要
mb_string
扩展的快速解决方案:或者修改 由@Alexander Konstantinov提供的解决方案:
A quick solution that doesn't need the
mb_string
extension:Or a modification of the solution provided by @Alexander Konstantinov:
此函数检查一个单词是否至少包含一个日语字母(我在 Wikipedia)。
This function checks whether a word contains at least one Japanese letter (I found unicode range for Japanese letters in Wikipedia).
你可以尝试一下Google的翻译API,它有一个检测功能:
http://code.google.com/apis/语言/翻译/v2/using_rest.html#检测语言
You could try Google's Translation API that has a detection function:
http://code.google.com/apis/language/translate/v2/using_rest.html#detect-language
如果编码是 EUC-,请尝试使用 mb_detect_encoding 函数JP 或 UTF-8 / UTF-16 可以是日语,否则是英语。
如果您能确保每种语言采用哪种编码,那就更好了,因为 UTF 编码可用于多种语言
Try with mb_detect_encoding function, if encoding is EUC-JP or UTF-8 / UTF-16 it can be japanese, otherwise english.
The better is if you can ensure which encoding each language, as UTF encodings can be used for many languages
英文文本通常仅包含 ASCII 字符(或者更好的说法是 ASCII 范围内的字符)。
English text usually consists only of ASCII characters (or better say, characters in ASCII range).
您可以尝试转换字符集并检查是否成功。
看看 iconv: http://www.php.net/manual/ en/function.iconv.php
如果您可以将字符串转换为 ISO-8859-1,它可能是英语,如果您可以转换为 iso-2022-jp,它可能是日语(我可能对确切的错误)字符集,你应该用谷歌搜索它们)。
You can try to convert the charset and check if it succeeds.
Take a look at iconv: http://www.php.net/manual/en/function.iconv.php
If you can convert a string to ISO-8859-1 it might be english, if you can convert to iso-2022-jp it is propably japanese (I might be wrong for the exact charsets, you should google for them).