0

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

2
  • Could you just loop through the string every 30 chars, if that spot isn't a space, go forward or backwards to the nearest space? Commented May 29, 2015 at 1:36
  • thanks for reply..trying to implement this in code now Commented May 29, 2015 at 1:45

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

OP should use this. Looks like this function has been around for a while and I have never used it. Learn something new every day.
@Austin : doesn't work for string "$str = "test string more and more terxt very long string can be upto length of 90 bla bla bla3rr33";" In such case it should chop the $str..i guess, i'll loop through the string for 30 chars and check for spaces as suggested by wade
@kennyben You are right; your problem is actually a bit different than what wordwrap does. I'll update my answer.
0

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.

4 Comments

sorry but comment provided by Wade makes more sense..just trying to implement the code. Thanks for reply anyways
@kennyben The above will always split it correctly via characters (words). Look at the example
yes, thank you. but if you read the question this is not what we are trying to achieve.
@kennyben Whoops, just read it properly, give me a second and I'll try whip something up quick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.