php - long text string splitting

Richard Korts
Richard Korts used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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?
Richard KortsBusiness Owner / Chief Developer

Author

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
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

NerdsOfTechTechnology Scientist

Commented:
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
NerdsOfTechTechnology Scientist

Commented:
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

NerdsOfTechTechnology Scientist

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial