Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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