Jump to content

detect chinese character


orange08

Recommended Posts

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.)

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 = "&#35813; blah &#30007; dah &#30340; we &#33394; doo &#38144;&#34507; waah &#31957;&#12290;";
$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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.