简体   繁体   English

“é”、“è”和所有带有重音符号的字母在 PHP function 中使用 PREG_REPLACE

[英]“é” , “è” and all letters with accents NOT DISPLAYED in a PHP function using PREG_REPLACE

I'm working on a search engine.我正在研究一个搜索引擎。 I found on the web a well written php function enabling a keyword listing from a text.我在 web 上找到了一个写得很好的 php function 启用文本中的关键字列表。 The function works perfectly in English. function 在英语中完美运行。 However when I tried to adapt it in French I observed that the "é", "è", "à" letters and all letters with accents are not displayed in the array output.但是,当我尝试用法语对其进行调整时,我发现数组 output 中没有显示“é”、“è”、“à”字母和所有带重音符号的字母。

For example, if the text contains: "Hello Héllo" =>=> Output = "Hello Hllo"例如,如果文本包含:"Hello Héllo" =>=> Output = "Hello Hllo"

I guess the issue is somewhere in the following code line:我猜问题出在以下代码行中:

$text = preg_replace('/[^a-zA-Z0-9 -.]/', '', $text); // only take alphanumerical characters, but keep the spaces and dashes too…

Any idea?任何想法? Thanks a lot from France !非常感谢法国!

Full code is the following:完整代码如下:

function generateKeywordsFromText($text){


// List of words NOT to be included in keywords
  $stopWords = array('à','à demi','à peine','à peu près','absolument','actuellement','ainsi');
  
  $text = preg_replace('/\s\s+/i', '', $text); // replace multiple spaces etc. in the text
  $text = trim($text); // trim any extra spaces at start or end of the text
  $text = preg_replace('/[^a-zA-Z0-9 -.]/', '', $text); // only take alphanumerical characters, but keep the spaces and dashes too…
  $text = strtolower($text); // Make the text lowercase so that output is in lowercase and whole operation is case in sensitive.

  // Find all words
  preg_match_all('/\b.*?\b/i', $text, $allTheWords);
  $allTheWords = $allTheWords[0];
  
  //Now loop through the whole list and remove smaller or empty words
  foreach ( $allTheWords as $key=>$item ) 
  {
      if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
          unset($allTheWords[$key]);
      }
  }   
  
  // Create array that will later have its index as keyword and value as keyword count.
  $wordCountArr = array();
  
  // Now populate this array with keywrds and the occurance count
  if ( is_array($allTheWords) ) {
      foreach ( $allTheWords as $key => $val ) {
          $val = strtolower($val);
          if ( isset($wordCountArr[$val]) ) {
              $wordCountArr[$val]++;
          } else {
              $wordCountArr[$val] = 1;
          }
      }
  }
  
  // Sort array by the number of repetitions
  arsort($wordCountArr);
  
  //Keep first 10 keywords, throw other keywords
  $wordCountArr = array_slice($wordCountArr, 0, 50);
  
  // Now generate comma separated list from the array
  $words="";
  foreach  ($wordCountArr as $key=>$value)
  $words .= " " . $key ;
  
  // Trim list of comma separated keyword list and return the list
  return trim($words," ");
  } 
  echo $contentkeywords = generateKeywordsFromText("Hello, Héllo");

You need to fix all your three preg_replace calls:您需要修复所有三个preg_replace调用:

$text = preg_replace('/\s{2,}/ui', '', $text); // replace multiple spaces etc. in the text
$text = preg_replace('/[^\p{L}0-9 .-]+/u', '', $text); // only take alphanumerical characters, but keep the spaces and dashes too…
// Find all words
preg_match_all('/\w+/u', $text, $allTheWords);

See the PHP demo请参阅PHP 演示

Details细节

  • '/\s{2,}/ui' - this will match any two or more Unicode whitespace chars '/\s{2,}/ui' - 这将匹配任何两个或多个 Unicode 空白字符
  • '/[^\p{L}0-9.-]+/u' - matches one or more chars other than any Unicode letter ( \p{L} ), any ASCII digit ( 0-9 ) or space, dot or hyphen (note the - must be used at the end of the character class) '/[^\p{L}0-9.-]+/u' - 匹配除任何 Unicode 字母 ( \p{L} )、任何 ASCII 数字 ( 0-9 ) 或空格、点之外的一个或多个字符或连字符(注意-必须在字符类的末尾使用)
  • '/\w+/u' matches all Unicode words, sequences of one or more letter/digit/underscore chars. '/\w+/u'匹配所有 Unicode 单词,一个或多个字母/数字/下划线字符的序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM