【问题标题】:How to split a long string without breaking words?如何在不破坏单词的情况下拆分长字符串?
【发布时间】:2012-06-29 00:44:08
【问题描述】:

我正在寻找类似的东西

str_split_whole_word($longString, $x)

其中$longString 是句子的集合,$x 是每行的字符长度。可能会比较长,我基本上想以数组的形式拆分成多行。

例如:

$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';
$lines = str_split_whole_word($longString, $x);

期望的输出:

$lines = Array(
    [0] = 'I like apple. You'
    [1] = 'like oranges. We'
    [2] = and so on...
)

【问题讨论】:

    标签: php arrays regex string split


    【解决方案1】:

    最简单的解决方案是在新行上使用wordwrap() 和explode(),如下所示:

    $array = explode( "\n", wordwrap( $str, $x));
    

    其中$x 是用于包装字符串的字符数。

    【讨论】:

    • 如果字符串中还没有换行符,这将非常有用。
    • 完美的 CLI 输出错误!谢谢
    【解决方案2】:

    我的要求是在每 20 个字符后拆分文本字符串,不要断词。使用wordwrap() 在每 20 个字符后插入换行符 所以这就是我的做法。希望这对找到此类解决方案的其他人有所帮助。

    $charactersLimit = 20;
    $yourTextString = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.';
    $output = explode("\n", wordwrap($yourTextString, $charactersLimit));
    

    输出如下:

    Array
    (
        [0] => Lorem ipsum dolor
        [1] => sit amet,
        [2] => consectetuer
        [3] => adipiscing elit.
    )
    

    【讨论】:

      【解决方案3】:

      只需一个preg_ 函数调用即可完成整个任务。

      1. 匹配零到$maxLength 次之间的任何字符。
      2. 使用\K 忘记/释放#1 中的匹配字符。
      3. 匹配下一个或多个空格字符或字符串的结尾。这里匹配的字符/位置会在拆分过程中被消耗,不会出现在输出数组中。
      4. 设置preg_函数,排除在字符串末尾用PREG_SPLIT_NO_EMPTY分割产生的空元素。

      代码:(Demo)

      $longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';
      $maxLength = 18;
      
      var_export(
          preg_split("/.{0,{$maxLength}}\K(?:\s+|$)/", $longString, 0, PREG_SPLIT_NO_EMPTY)
      );
      

      输出:

      array (
        0 => 'I like apple. You',
        1 => 'like oranges. We',
        2 => 'like fruit. I like',
        3 => 'meat, also.',
      )
      

      如果您的输入字符串可能包含换行符,则只需添加 s 模式修饰符即可。
      /.{0,{$maxLength}}\K(?:\s+|$)/s (Demo)

      【讨论】:

        【解决方案4】:

        使用wordwrap() 插入换行符,然后在这些换行符处使用explode():

        // Wrap at 15 characters
        $x = 15;
        $longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';
        $lines = explode("\n", wordwrap($longString, $x));
        
        var_dump($lines);
        array(6) {
          [0]=>
          string(13) "I like apple."
          [1]=>
          string(8) "You like"
          [2]=>
          string(11) "oranges. We"
          [3]=>
          string(13) "like fruit. I"
          [4]=>
          string(10) "like meat,"
          [5]=>
          string(5) "also."
        }
        

        【讨论】:

          【解决方案5】:

          这段代码避免了断字,你不会使用 wordwrap() 得到它。

          最大长度使用$maxLineLength 定义。我做了一些测试,效果很好。

          $longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';
          
          $words = explode(' ', $longString);
          
          $maxLineLength = 18;
          
          $currentLength = 0;
          $index = 0;
          
          foreach ($words as $word) {
              // +1 because the word will receive back the space in the end that it loses in explode()
              $wordLength = strlen($word) + 1;
          
              if (($currentLength + $wordLength) <= $maxLineLength) {
                  $output[$index] .= $word . ' ';
                  $currentLength += $wordLength;
              } else {
                  $index += 1;
                  $currentLength = $wordLength;
                  $output[$index] = $word;
              }
          }
          

          【讨论】:

          • 这是不正确的。它会删除一些单词之间的空格。
          • 这个答案是否正确?它会生成警告并破坏输出! 3v4l.org/Ref5f
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-20
          • 2011-08-27
          • 2021-01-05
          • 2013-03-14
          • 2011-10-05
          • 2019-10-04
          相关资源
          最近更新 更多