Take the 2-minute tour ×
$str = "This is a string containing 中文 characters. Some more characters - 中华人民共和国 ";

How do I detect chinese characters from this string and print the part which starts with the first character and ends with "-"? (it would be "中文 characters. Some more characters -").

Thank you!

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

I've solved this problem using preg_match and regular expressions:


preg_match(/[\x{4e00}-\x{9fa5}]+.*\-/u, $str, $matches);
share|improve this answer
Thanks for this... curious, where is the ability to use \x{unicode#} documented? –  philfreo Oct 19 '10 at 6:49
@philfreo: it's here: php.net/manual/en/regexp.reference.escape.php –  Savageman Jul 30 '11 at 12:20
add comment

Is PHP storing this as Unicode? If so, at worst you could step through the string, character by character, until you hit those within the Chinese range.

Check this out too PHP: Unicode - Manual

share|improve this answer
@Josh - if you follow boost's suggestion, to might like to also look at VonC's answer to this question: stackoverflow.com/questions/1366068/… –  JV. Oct 11 '09 at 15:53
@boost, yes, php is storing the string in unicode. But how do I accomplish it? I'm not very good in php. @JV, thanks, I'll take a look at it. –  Josh Oct 11 '09 at 16:06
if you do not convert it to NCR form, it has chance to corrupt the characters during transactions. –  Raptor Oct 20 '09 at 7:55
add comment

Your Answer

