热搜:NVER 

求1PHP正则表达式

2024-04-26 15:10:01
 求1PHP正则表达式

求一PHP正则表达式
比如有这样一段话“求一PHP正则表达式like,フレ┼ズ”,中英日文字都有!
我要将这句话的每个字存到一个数组里,其中英文单词存一个单元,
比如{"求","一","PHP","正则","表","达","式","like","フ","レ","┼","ズ"}


网上找了段代码可以将中文都存到一个数组

//用正则匹配半角单个字符或者全角单个字符,存入数组$ar
preg_match_all("/[\x80-\xff]+?\\x00/",$str,$ar);  
$ar = $ar[0];
//去掉$ar中ASCII为0字符的项目
for ( $i = 0; $i   if ($ar[$i] != chr(0x00)) {
$ar_new[]=$ar[$i];
echo "==".$ar[$i];
  }
}

------解决方案--------------------
$s = '求一PHP正则表达式like,フレ?ズ';
preg_match_all("/[\x80-\xff].|\w+/", $s, $r);
print_r($r[0]);

Array ( [0] => 求 [1] => 一 [2] => PHP [3] => 正 [4] => 则 [5] => 表 [6] => 达 [7] => 式 [8] => like [9] => フ [10] => レ [11] => ズ )
------解决方案--------------------
这个问题涉及到中英文分词了吧?
------解决方案--------------------
是"正则"还是"正","则"?这个区别有点大的。
------解决方案--------------------

PHP code

<?php $str = '"求","一","PHP","正则","表","达","式","like","フ","レ","┼","ズ"';
$str = str_ireplace(array(',', '"'), array("", ""), $str);
$pattern = "/[^\x4e00-\x9fa5]{2}|[\w]+/i";
preg_match_all($pattern, $str, $aMatch);
print_r($aMatch);
?>