【问题标题】:str_split without word-wrapstr_split 没有自动换行
【发布时间】:2012-03-27 17:28:25
【问题描述】:

我正在寻找最快的解决方案,将 一个字符串分成几部分,而不需要

$strText = "The quick brown fox jumps over the lazy dog";

$arrSplit = str_split($strText, 12);

// result: array("The quick br","own fox jump","s over the l","azy dog");
// better: array("The quick","brown fox","jumps over the","lazy dog");

【问题讨论】:

    标签: split word-wrap php string split word-wrap


    【解决方案1】:

    您实际上可以使用wordwrap(),输入explode(),使用换行符\n 作为分隔符。 explode() 将在wordwrap() 生成的换行符上拆分字符串。

    $strText = "The quick brown fox jumps over the lazy dog";
    
    // Wrap lines limited to 12 characters and break
    // them into an array
    $lines = explode("\n", wordwrap($strText, 12, "\n"));
    
    var_dump($lines);
    array(4) {
      [0]=>
      string(9) "The quick"
      [1]=>
      string(9) "brown fox"
      [2]=>
      string(10) "jumps over"
      [3]=>
      string(12) "the lazy dog"
    }
    

    【讨论】:

    • 注意:使用 false (默认)作为第四个参数可以防止换行时单词被破坏。正是我需要的。如果您不关心破坏单词,请将其设置为 true。
    猜你喜欢
    • 2013-07-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多