php - long text string splitting

See this code:
$piece1 = $test;
$piece2 = "";
$piece3 = "";
$ln = strlen($test);
if ($ln > 84) {

	$arrayWords = explode(' ', $test);

// Max size of each line
	$maxLineLength = 84;

// Auxiliar counters, foreach will use them
	$currentLength = 0;
	$index = 0;

	foreach ($arrayWords 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) {
			$arrayOutput[$index] .= $word . ' ';

			$currentLength += $wordLength;
		} else {
			$index += 1;

			$currentLength = $wordLength;

			$arrayOutput[$index] = $word . ' ';
		}
	}
	$piece1 = $arrayOutput[0];
	$piece2 = $arrayOutput[1];
	$piece3 = $arrayOutput[2];
}	

Open in new window


This works perfectly, except I need a variation where I can limit the FIRST line ($arrayOutput[0]) to 84 & the subsequent lines to 94. See attached image for visual of this need. The text is printed using a pdf generator on an existing form image (with the word Remarks & the lines.)

How can I do this?

Thank you
Capture.JPG
Richard KortsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Usually the process for doing this is to check from char 84 and work backwards until a line breaking char is found (white space).
The question is if the first line breaks at 76 must we then count the next 94 chars from char 77 of line 1 added to the next line?
0
Richard KortsAuthor Commented:
Julian,

I see where you are coming from. But the beauty of this solution (I found it via Google) is that it breaks all words on spaces into arrays, then builds the strings back up..

I think I can just set $maxLineLength to 84 initially, then after $index is > zero, set it to 94.

Thanks,

Richard
0
Julian HansenCommented:
I see where you are going - one way of doing this is to have an array of  lengths. On each iteration you shift the next number out of the array

$length = [84,94];
$maxLineLength = count($length) ? array_shift($length) : $maxLineLength ;

Open in new window


This code will default the value to the last number in the array if there are no more $length's available which means you don't have to specify a length for every line.
EDIT
Fixed array definition - changed . to a ,
and spelling error in $length
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

NerdsOfTechTechnology ScientistCommented:
If the first restriction (84 characters) were allowed on the next two lines it could be 3 lines of code:

$test = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.';
$wrap = wordwrap($test, 84);
$pieces = explode("\n", $wrap); //$pieces[0] first line, [1] second, [2] third.

Open in new window


http://php.net/manual/en/function.wordwrap.php
0
NerdsOfTechTechnology ScientistCommented:
If we extract the length of the first wrap, we can calculate the start of the secondary wrap and pull this off with minimum code. All overflow (>line3 is truncated).

<?php
$test = 'test'; // line 1 test
$test = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal'; //max line 2 test, the period pushes it to 3 lines
$test = 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war.';

$wrap = wordwrap($test, 84);
$pieces = explode("\n", $wrap);
// print_r($pieces);
$piece1 = $pieces[0];
//debug
echo '<br>line 1 [' . strlen($piece1). ']: ' . $piece1;
$ln = strlen($pieces[0]);
$extra = substr($test, $ln); //return all but line 1

$wrap = wordwrap($extra, 94);
$pieces = explode("\n", $wrap); //[0] line2, [1] line3
// print_r($pieces);
if(!empty($pieces)){
	//debug
	$piece2 = ltrim($pieces[0]); //remove first space in beginning
 echo '<br>line 2 [' . strlen($piece2). ']: ' . $piece2;
 if(isset($pieces[1])){
  $piece3 = $pieces[1];
	 //debug
	 echo '<br>line 3 [' . strlen($piece3). ']: ' . $piece3;
 }
}

//debug
echo '<br>test [' . strlen($test). ']: ' . $test;

Open in new window

0
NerdsOfTechTechnology ScientistCommented:
It seems as if there was no need to test my pure PHP solution #a42369541  ?

Can you post the code you found from Google that helped with the solution here?

Nonetheless, I hope someone will get to admire the power of the PHP wordwrap() function in this solution.

Best regards.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.