larka06 Posted August 21, 2013 Share Posted August 21, 2013 The output below is what I get if I put the echo statement anywhere but in thewhile Loop. Just before the while Loop is entered both $i and $numLines have 5 foran answer. Therefore the while Loop runs once and only one line is Justified. Ihave tried to set $i to zero just before entering the loop still runs only once.I cannot get even one echo from inside the while Loop. <?php // The text to justify $myText = <<<END_TEXT But think not that this famous town has only harpooneers, cannibals, and bumpkins to show her visitors. Not at all. Still New Bedford is a queer place. Had it nt been for us whalemen, that tract of land would this day perhaps have been in howling condition as the coast of Labador. END_TEXT; $myText = str_replace("\r\n", "\n", $myText); $lineLength = 60; // desired line lenght $myTextJustified = " "; $numLines = substr_count($myText,"\n");$startOfLine = 0; echo $numLines; //Move through each line in turn for($i=0; $i < $numLines; $i++) $originalLineLength = strpos($myText, "\n", $startOfLine) - $startOfLine; $justifiedLine = substr($myText, $startOfLine, $originalLineLength); $justifiedLineLength = $originalLineLength; echo $numLines; echo $i // Keep adding spaces between words until the desired line length is reached while($i < $numLines -1 && $justifiedLineLength < $lineLength) { echo $i; for($j=0; $j< $justifiedLineLength; $j++){ if($justifiedLineLength < $lineLength && $justifiedLine[$j] == " ") { $justifiedLine =substr_replace( $justifiedLine, " ", $j, 0); $justifiedLineLength++; $j++; } } } // Add the justified line to the string and move to the start of the next line $myTextJustified .= "$justifiedLine\n"; $startOfLine += $originalLineLength + 1; ?> Here is the output. Justifying Lines of Text55Original text:But think not that this famous town has only harpooneers, cannibals,and bumpkins to show her visitors. Not at all. StillNew Bedford is a queer place. Had it nt been for us whalemen,that tract of land would this day perhaps have been inhowling condition as the coast of Labador.Justfied text: But think not that this famous town has only harpooneers, cannibals, Link to comment https://forums.phpfreaks.com/topic/281413-wrox-is-back-in-need-of-help/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.