orange08 Posted February 15, 2009 Share Posted February 15, 2009 if i have a sentence mixed up with english and chinese words, so how can i detect a word is chinese or english? thanks! Link to comment https://forums.phpfreaks.com/topic/145276-detect-chinese-character/ Share on other sites More sharing options...
supposedlysupposed Posted February 15, 2009 Share Posted February 15, 2009 I don't know anything about the Chinese language, but if no Chinese words have English characters, then you could do it the opposite way, detect which words are English, and assume all others are Chinese. It might be something along the lines of: $text = "该 blah 男 dah 的 we 色 doo 销蛋 waah 糕。"; $words = explode(" ", $text); foreach($words as $key => $val) { if(preg_match("/[a-zA-Z]/", $val)) { echo $val; // Probably an English word } else { echo "<u>".$val."</u>"; // Probably a Chinese word } } This underlines all Chinese words in the output of the code. (Technically, just every word without a-z in it.) Link to comment https://forums.phpfreaks.com/topic/145276-detect-chinese-character/#findComment-762645 Share on other sites More sharing options...
orange08 Posted February 15, 2009 Author Share Posted February 15, 2009 I don't know anything about the Chinese language, but if no Chinese words have English characters, then you could do it the opposite way, detect which words are English, and assume all others are Chinese. It might be something along the lines of: $text = "该 blah 男 dah 的 we 色 doo 销蛋 waah 糕。"; $words = explode(" ", $text); foreach($words as $key => $val) { if(preg_match("/[a-zA-Z]/", $val)) { echo $val; // Probably an English word } else { echo "<u>".$val."</u>"; // Probably a Chinese word } } This underlines all Chinese words in the output of the code. (Technically, just every word without a-z in it.) thanks, the idea is good! actually the main purpose for me to have this logic is i want to split the chinese character. for your information, the explode() will take few chinese characters as one word like '公司', but the actual chinese not treat it as one word, but two words. so, i need to split it up to '公',and '司'. do you have any idea? Link to comment https://forums.phpfreaks.com/topic/145276-detect-chinese-character/#findComment-762659 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.