Jump to content

wrox is back IN need of help


larka06

Recommended Posts

The output below is what I get if I put the echo statement anywhere but in the
while Loop. Just before the while Loop is entered both $i and $numLines have 5 for
an answer. Therefore the while Loop runs once and only one line is Justified. I
have 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 Text
55
Original 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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.