简体   繁体   English

将字符串拆分到新行

[英]Split string to new line

I am trying to split a 90 character long string onto three 30 length strings. 我正在尝试将90个字符长的字符串拆分为三个30个长度的字符串。 I cannot simply chop the long string into three lines(worst case scenario) as words in string will split. 我不能简单地将长字符串切成三行(最坏的情况),因为字符串中的单词会分裂。 The code I have tried is as follows(but doesn't work), please help 我尝试过的代码如下(但不起作用),请帮忙

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

    $str = "test string more and more text very long string can be upto length of 90";
    $line1 = $line2 = $line3 = "";

    $temp = explode(" ", $str);
    //var_dump($temp);

    for($i=0; $i<count($temp); $i++){
        if(strlen($line1) < 30){
            $line1. = $temp[$i]." ";

        }else if(strlen($line2) < 30) {
            $line2. = $temp[$i]." ";

        }else if(strlen($line3) < 30) {
            $line2. = $temp[$i]." ";

        }
    }

    //var_dump($line1);
?>

I am trying to add as many words in $line1 from $temp as possible and then to $line2 .... 我正在尝试从$ temp尽可能多地在$ line1中添加单词,然后再添加至$ line2...。

Normally, I would say that that's the wordwrap function is for: 通常,我想说的是自动wordwrap功能用于:

$str = "test string more and more text very long string can be upto length of 90";
$wrapped = wordwrap($str, 30, "\n", true);
list($line1, $line2, $line3) = explode("\n", $wrapped . "\n\n");

(The extra \\n s on $wrapped are to prevent errors in case fewer than 3 lines are made.) $wrapped上的额外\\n可以防止出现少于3行的错误。)

However, your problem is a bit different. 但是,您的问题有所不同。 wordwrap will sometimes make a 4th line rather than cut a word in half, which is what you need to do. wordwrap有时会排成第四行,而不是将单词切成两半,这是您需要做的。 wordwrap will only ever make a 4th line if cutting a word is required, so perhaps try this: 如果需要剪切单词,则自动wordwrap只会排在第四行,因此,请尝试以下操作:

$str = "test string more and more terxt very long string can be upto length of 90 bla bla bla3rr33";
$maxLen = 30;
$wrapped = wordwrap($str, $maxLen, "\n", true);
list($line1, $line2, $line3, $line4) = explode("\n", $wrapped . "\n\n\n");
if ($line4 !== '') {
    //fallback case: we have to split words in order to fit the string properly in 3 lines
    list($line1, $line2, $line3) = array_map('trim', str_split($str, $maxLen));
}

There is one bad thing that this code can do: it will sometimes split two words when it only needs to split one. 此代码可以做一件坏事:有时只需要拆分一个单词,有时就会拆分两个单词。 I'll leave it to you to figure out how to fix that if you need to. 我将留给您了解如何解决此问题。

Here's an alternative, assuming all of the words are separated by spaces. 这是一个替代方案,假设所有单词都用空格分隔。

$words = explode(" ", $string);
$chunks = array_chunk($words, 30);

foreach($chunks as $chunk) {
    echo implode(" ", $chunk) . "\n\n\n";    
}

We're harnessing explode() , array_chunk() and implode() to do as required. 我们利用explode()array_chunk()implode()进行所需的操作。

Example


The way it works above is as follows: 上面的工作方式如下:

  • Take the string and split it into an array of words (with the delimiter being the empty space - " " ) 取字符串并将其拆分为单词数组(定界符为空白- " "
  • Now we'll array_chunk ( chunk ) the array into 3 seperate arrays of 30 items. 现在,我们将array_chunk )数组分成3个独立的数组,每个数组包含30个项目。
  • Now we'll loop through that array of chunks and implode() the array, making it a string again with the same space delimiter - " " . 现在,我们将遍历该块数组并implode()将该数组再次变为具有相同空格分隔符- " "的字符串。

And viola, you've chunked it and split the string as you desire. 中提琴,您已经将它切成块并根据需要拆分字符串。

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

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