Ich bin ein Anfänger bei PHP und habe einen Code geschrieben, um einen Text im Google Chrome Browser zu rechtfertigen. Hier ist der Code:Warum funktioniert mein PHP-Programm nicht?
<!DOCTYPE html>
<html>
<head>
<title>Text Justification with PHP</title>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<h1>Text Justification with PHP</h1>
<?php
$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;
$lineLength = 40;
$myText = str_replace("\r\n", "\n", $myText);
$myTextJustified = "";
$numLines = substr_count($myText, "\n");
$startOfLine = 0;
for ($i = 0; $i < $numLines; $i++){
$originalLineLength = strpos($myText, "\n", $startOfLine) - $startOfLine;
$justifiedLine = substr($myText, $startOfLine, $originalLineLength);
$justifiedLineLength = $originalLineLength;
while ($i < $numLines - 1 && $justifiedLineLength < $lineLength) {
for($j = 0; $j < $justifiedLineLength; $j++){
if ($justifiedLineLength < $lineLength && $justifiedLine[$j] == " ") {
$justifiedLine = substr_replace($justifiedLine, " ", $j, 0);
$justifiedLineLength++;
$j++;
}
}
}
$myTextJustified .= "$justifiedLine\n";
$startOfLine += $originalLineLength + 1;
}
?>
<h3>Original Text:</h3>
<p><?php echo str_replace("\n", "<br>", $myText); ?></p><br><br>
<h3>Justified Text:</h3>
<p><?php echo str_replace("\n", "<br>", $myTextJustified); ?></p>
</body>
</html>
Aber der Text dreht sich nicht gerechtfertigt und beide meine Absätze sind völlig gleich. Hat das etwas mit der Browserkompatibilität zu tun? Kann mir das jemand sagen? Danke für ihre Aufmerksamkeit.
warum nicht css verwenden? –