Working with Strings Function Try It Out page : 83
I wrote this code and I checked it many times but it does not work all what I have on the screen is the ( Justifying Lines of Text ) text!!!!!! which is the first html code.
anybody know what is the problem, two people always better than one.
thanks.
<?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 not been for us whalemen, that
tract of land would this day perhaps
have been in as howling condition as the
coast of Labrador.
END_TEXT;
$myText = str_replace ("\r\n", "\n", $myText);
$lineLength = 40;
$myTextJustfied = " ";
$numLines = substr_count($myText, "\n");
$startOfLine = 0;
for ($i= 0; $i< $numLines; $i++){
$orejLineLength = strpos($myText, "\n", $startOfLine) - $startOfLine;
$justfiedLine = substr($myText, $startOfLine, $orejLineLength);
$justFiedLineLength = $orejLineLength;
while ( $i < $numLines -1 && $justFiedLineLength < $lineLength ) {
for ( $j =0; $j <$justFiedLineLength; $j++) {
if ( $justFiedLineLength < $lineLength && $justfiedLine[$j] == " " ) {
$justfiedLine = substr_replace ( $justfiedLine, " ", $j, 0);
$justfiedLineLength++;
$j++;
}
}
}
$myTextJustfied .= "$justfiedLine\n";
$startOfLine += $orejLineLength +1;
}
?>
<h2>Original text:</h2>
<pre><?php echo $myText ?></pre>
<h2>Justified text:</h2>
<pre><?php echo $myTextJustfied ?></pre>
</body>
</html>
|