Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php中的文本中提取西里尔语术语/关键字_Php - Fatal编程技术网

从php中的文本中提取西里尔语术语/关键字

从php中的文本中提取西里尔语术语/关键字,php,Php,我正在尝试为我的网页建立关键字,我想从文本中提取这些关键字。 我有这个功能 function extractCommonWords($string){ $stopWords = array('и', 'или'); $string = preg_replace('/ss+/i', '', $string); $string = trim($string); $string = preg_replace('/[^a-zA-Z0-9 -]/', ''

我正在尝试为我的网页建立关键字,我想从文本中提取这些关键字。 我有这个功能

function extractCommonWords($string){
     $stopWords = array('и', 'или');

      $string = preg_replace('/ss+/i', '', $string);
      $string = trim($string); 
      $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); 
      $string = strtolower($string); 
      preg_match_all('/\b.*?\b/i', $string, $matchWords);
      $matchWords = $matchWords[0];

      foreach ( $matchWords as $key=>$item ) {
          if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
              unset($matchWords[$key]);
          }
      }  
      $wordCountArr = array();
      if ( is_array($matchWords) ) {
          foreach ( $matchWords as $key => $val ) {
              $val = strtolower($val);
              if ( isset($wordCountArr[$val]) ) {
                  $wordCountArr[$val]++;
              } else {
                  $wordCountArr[$val] = 1;
              }
          }
      }
      arsort($wordCountArr);
      $wordCountArr = array_slice($wordCountArr, 0, 10);
      return $wordCountArr;
}

问题是不能用西里尔字母。如何解决这个问题?

西里尔字母是多字节字符。您需要使用PHP

对于正则表达式,您需要添加
/u
修饰符以使其符合unicode


另请参见

要替换的模式也将删除所有西里尔字母,因为
a-z
将不匹配它们

将此添加到character类以保留西里尔字符:

\p{Cyrillic}
…并使用GolezTrol建议的类似修改器
u

$string = preg_replace('/[^\p{Cyrillic} a-zA-Z0-9 -]/u', '', $string); 
如果您只想提取西里尔文字,则无需替换任何内容,只需使用以下内容匹配文字:

preg_match_all('/\b(\p{Cyrillic}+)\b/u', $string, $matchWords);
preg_match_all('/\b(\p{Cyrillic}+)\b/u', $string, $matchWords);