如何使用PHP检查单词是日语还是英语 [英] How to check if the word is Japanese or English using PHP

查看:98
本文介绍了如何使用PHP检查单词是日语还是英语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望此功能对英语单词和日语单词有不同的处理方式

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) {
      ////////
   }
}

谢谢

推荐答案

一种不需要mb_string扩展名的快速解决方案:

A quick solution that doesn't need the mb_string extension:

if (strlen($str) != strlen(utf8_decode($str))) {
    // $str uses multi-byte chars (isn't English)
}

else {
    // $str is ASCII (probably English)
}

或对

Or a modification of the solution provided by @Alexander Konstantinov:

function isKanji($str) {
    return preg_match('/[\x{4E00}-\x{9FBF}]/u', $str) > 0;
}

function isHiragana($str) {
    return preg_match('/[\x{3040}-\x{309F}]/u', $str) > 0;
}

function isKatakana($str) {
    return preg_match('/[\x{30A0}-\x{30FF}]/u', $str) > 0;
}

function isJapanese($str) {
    return isKanji($str) || isHiragana($str) || isKatakana($str);
}

这篇关于如何使用PHP检查单词是日语还是英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆