分离字符串中的中英文语句
$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) "没有人知道." }
转载请注明原处