• <legend id='IQTck'><style id='IQTck'><dir id='IQTck'><q id='IQTck'></q></dir></style></legend>
    • <bdo id='IQTck'></bdo><ul id='IQTck'></ul>

      <small id='IQTck'></small><noframes id='IQTck'>

      <i id='IQTck'><tr id='IQTck'><dt id='IQTck'><q id='IQTck'><span id='IQTck'><b id='IQTck'><form id='IQTck'><ins id='IQTck'></ins><ul id='IQTck'></ul><sub id='IQTck'></sub></form><legend id='IQTck'></legend><bdo id='IQTck'><pre id='IQTck'><center id='IQTck'></center></pre></bdo></b><th id='IQTck'></th></span></q></dt></tr></i><div id='IQTck'><tfoot id='IQTck'></tfoot><dl id='IQTck'><fieldset id='IQTck'></fieldset></dl></div>

        <tfoot id='IQTck'></tfoot>

        将文本拆分为单个单词

        时间:2023-10-03
          <i id='4s4AJ'><tr id='4s4AJ'><dt id='4s4AJ'><q id='4s4AJ'><span id='4s4AJ'><b id='4s4AJ'><form id='4s4AJ'><ins id='4s4AJ'></ins><ul id='4s4AJ'></ul><sub id='4s4AJ'></sub></form><legend id='4s4AJ'></legend><bdo id='4s4AJ'><pre id='4s4AJ'><center id='4s4AJ'></center></pre></bdo></b><th id='4s4AJ'></th></span></q></dt></tr></i><div id='4s4AJ'><tfoot id='4s4AJ'></tfoot><dl id='4s4AJ'><fieldset id='4s4AJ'></fieldset></dl></div>
            <tbody id='4s4AJ'></tbody>

              <legend id='4s4AJ'><style id='4s4AJ'><dir id='4s4AJ'><q id='4s4AJ'></q></dir></style></legend>

              1. <small id='4s4AJ'></small><noframes id='4s4AJ'>

                  <bdo id='4s4AJ'></bdo><ul id='4s4AJ'></ul>
                  <tfoot id='4s4AJ'></tfoot>

                  本文介绍了将文本拆分为单个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想使用 PHP 将文本拆分为单个单词.您知道如何实现这一目标吗?

                  I would like to split a text into single words using PHP. Do you have any idea how to achieve this?

                  我的方法:

                  function tokenizer($text) {
                      $text = trim(strtolower($text));
                      $punctuation = '/[^a-z0-9äöüß-]/';
                      $result = preg_split($punctuation, $text, -1, PREG_SPLIT_NO_EMPTY);
                      for ($i = 0; $i < count($result); $i++) {
                          $result[$i] = trim($result[$i]);
                      }
                      return $result; // contains the single words
                  }
                  $text = 'This is an example text, it contains commas and full-stops. Exclamation marks, too! Question marks? All punctuation marks you know.';
                  print_r(tokenizer($text));
                  

                  这是一个好方法吗?你有什么改进的想法吗?

                  Is this a good approach? Do you have any idea for improvement?

                  提前致谢!

                  推荐答案

                  使用匹配任何 unicode 标点符号的类 p{P},结合 s 空白类.

                  Use the class p{P} which matches any unicode punctuation character, combined with the s whitespace class.

                  $result = preg_split('/((^p{P}+)|(p{P}*s+p{P}*)|(p{P}+$))/', $text, -1, PREG_SPLIT_NO_EMPTY);
                  

                  这将拆分为一组一个或多个空白字符,但也会吸收任何周围的标点符号.它还匹配字符串开头或结尾的标点字符.这会区分诸如不要"和他说‘哎哟!’"之类的情况

                  This will split on a group of one or more whitespace characters, but also suck in any surrounding punctuation characters. It also matches punctuation characters at the beginning or end of the string. This discriminates cases such as "don't" and "he said 'ouch!'"

                  这篇关于将文本拆分为单个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP 将字符串拆分为整数元素和字符串 下一篇:PHP:在逗号上拆分字符串,但不在大括号或引号之

                  相关文章

                  最新文章

                        <bdo id='yjHU0'></bdo><ul id='yjHU0'></ul>
                      <tfoot id='yjHU0'></tfoot><legend id='yjHU0'><style id='yjHU0'><dir id='yjHU0'><q id='yjHU0'></q></dir></style></legend>
                    1. <i id='yjHU0'><tr id='yjHU0'><dt id='yjHU0'><q id='yjHU0'><span id='yjHU0'><b id='yjHU0'><form id='yjHU0'><ins id='yjHU0'></ins><ul id='yjHU0'></ul><sub id='yjHU0'></sub></form><legend id='yjHU0'></legend><bdo id='yjHU0'><pre id='yjHU0'><center id='yjHU0'></center></pre></bdo></b><th id='yjHU0'></th></span></q></dt></tr></i><div id='yjHU0'><tfoot id='yjHU0'></tfoot><dl id='yjHU0'><fieldset id='yjHU0'></fieldset></dl></div>

                      <small id='yjHU0'></small><noframes id='yjHU0'>