宗小宗-PHP程序猿

欢迎各位朋友前来,如果我的文章中有不对的地方,还望指正;如需转载,请注明原处

分离字符串中的中英文语句

            $string = "67. No one knows . 没有人知道。";
			$s = str_replace("。", ".", $string);
			$s = str_replace(".", ".", $s);
			$s = str_replace("’", "'", $s);
			$s = str_replace(",", ",", $s);
			$s = str_replace("“", '"', $s);
			$s = str_replace("”", '"', $s);
			$s = str_replace(",", ",", $s);
			$len = mb_strlen($s,'UTF-8');

			$j = 0;$k = 0;
			for($i=0;$i<$len;$i++){
				$str = mb_substr($s, $i,1,'UTF-8'); 
				if ($k==0&&preg_match('/[a-zA-Z]/', $str)) { 
					$j = $i;$k=1;
				}
				if (!empty($str)&&preg_match('/[^\x00-\x80]/', $str)) { 
					break; 
				}
			}
			$one['str'] = $s;
			$one['en'] = mb_substr($s, $j,$i-$j,'UTF-8');
			$one['zh'] = str_replace(" ", "", mb_substr($s, $i,$len-$i,'UTF-8'));
			var_dump($one);            

  结果

 

array(3) {
  ["str"]=>
  string(35) "67. No one knows . 没有人知道."
  ["en"]=>
  string(15) "No one knows . "
  ["zh"]=>
  string(16) "没有人知道."
}

  

posted @ 2016-02-23 23:27  宗小宗  阅读(429)  评论(0编辑  收藏  举报