kennyben
kennyben

Reputation: 5

Split string to new line

I am trying to split a 90 character long string onto three 30 length strings. 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 ....

Upvotes: 0

Views: 1239

Answers (2)

Austin
Austin

Reputation: 3328

Normally, I would say that that's the wordwrap function is for:

$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 \ns on $wrapped are to prevent errors in case fewer than 3 lines are made.)

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 will only ever make a 4th line if cutting a word is required, so perhaps try this:

$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.

Upvotes: 2

Darren
Darren

Reputation: 13128

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.

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.
  • Now we'll loop through that array of chunks and implode() the array, making it a string again with the same space delimiter - " ".

And viola, you've chunked it and split the string as you desire.

Upvotes: 0

Related Questions