<bdo id='2vqNG'></bdo><ul id='2vqNG'></ul>
    <tfoot id='2vqNG'></tfoot>
  • <small id='2vqNG'></small><noframes id='2vqNG'>

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

  • <legend id='2vqNG'><style id='2vqNG'><dir id='2vqNG'><q id='2vqNG'></q></dir></style></legend>

      1. 使用 PHP 将字符串分成两半(字识别)

        时间:2023-10-03
      2. <small id='X9tBI'></small><noframes id='X9tBI'>

        • <bdo id='X9tBI'></bdo><ul id='X9tBI'></ul>

              • <legend id='X9tBI'><style id='X9tBI'><dir id='X9tBI'><q id='X9tBI'></q></dir></style></legend>
              • <tfoot id='X9tBI'></tfoot>
                  <tbody id='X9tBI'></tbody>
                <i id='X9tBI'><tr id='X9tBI'><dt id='X9tBI'><q id='X9tBI'><span id='X9tBI'><b id='X9tBI'><form id='X9tBI'><ins id='X9tBI'></ins><ul id='X9tBI'></ul><sub id='X9tBI'></sub></form><legend id='X9tBI'></legend><bdo id='X9tBI'><pre id='X9tBI'><center id='X9tBI'></center></pre></bdo></b><th id='X9tBI'></th></span></q></dt></tr></i><div id='X9tBI'><tfoot id='X9tBI'></tfoot><dl id='X9tBI'><fieldset id='X9tBI'></fieldset></dl></div>
                  本文介绍了使用 PHP 将字符串分成两半(字识别)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试将字符串分成两半,它不应该在单词中间分开.

                  I'm trying to split strings in half, and it should not split in the middle of a word.

                  到目前为止,我想出了以下 99% 的工作:

                  So far I came up with the following which is 99% working :

                  $text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";
                  $half = (int)ceil(count($words = str_word_count($text, 1)) / 2); 
                  $string1 = implode(' ', array_slice($words, 0, $half));
                  $string2 = implode(' ', array_slice($words, $half));
                  

                  这确实有效,根据字符串中的单词数将任何字符串正确地分成两半.但是,它正在删除字符串中的任何符号,例如对于上面的示例,它将输出:

                  This does work, correctly splitting any string in half according to the number of words in the string. However, it is removing any symbols in the string, for example for the above example it would output :

                  The Quick Brown Fox Jumped
                  Over The Lazy Dog
                  

                  我需要在拆分后保留字符串中的所有符号,例如 : 和/.我不明白为什么当前的代码要删除符号...如果您能提供替代方法或修复此方法以不删除符号,将不胜感激:)

                  I need to keep all the symbols like : and / in the string after being split. I don't understand why the current code is removing the symbols... If you can provide an alternative method or fix this method to not remove symbols, it would be greatly appreciated :)

                  推荐答案

                  在查看您的示例输出时,我注意到我们所有的示例都关闭了,如果字符串的中间在一个单词内,我们将减少给 string1然后给予更多.

                  Upon looking at your example output, I noticed all our examples are off, we're giving less to string1 if the middle of the string is inside a word rather then giving more.

                  例如The Quick : Brown Fox Jumped Over The Lazy/Dog的中间是The Quick : Brown Fox Ju,它在一个词的中间,这个第一个示例为 string2 提供了拆分词;下面的例子给出了 string1 的分割词.

                  For example the middle of The Quick : Brown Fox Jumped Over The Lazy / Dog is The Quick : Brown Fox Ju which is in the middle of a word, this first example gives string2 the split word; the bottom example gives string1 the split word.

                  在拆分词上给 string1 少

                  $text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";
                  
                  $middle = strrpos(substr($text, 0, floor(strlen($text) / 2)), ' ') + 1;
                  
                  $string1 = substr($text, 0, $middle);  // "The Quick : Brown Fox "
                  $string2 = substr($text, $middle);  // "Jumped Over The Lazy / Dog"
                  

                  在拆分词上给 string1 更多

                  $text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";
                  
                  $splitstring1 = substr($text, 0, floor(strlen($text) / 2));
                  $splitstring2 = substr($text, floor(strlen($text) / 2));
                  
                  if (substr($splitstring1, 0, -1) != ' ' AND substr($splitstring2, 0, 1) != ' ')
                  {
                      $middle = strlen($splitstring1) + strpos($splitstring2, ' ') + 1;
                  }
                  else
                  {
                      $middle = strrpos(substr($text, 0, floor(strlen($text) / 2)), ' ') + 1;    
                  }
                  
                  $string1 = substr($text, 0, $middle);  // "The Quick : Brown Fox Jumped "
                  $string2 = substr($text, $middle);  // "Over The Lazy / Dog"
                  

                  这篇关于使用 PHP 将字符串分成两半(字识别)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 PHP 拆分大文件 下一篇:str_split 没有自动换行

                  相关文章

                  最新文章

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

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

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