$result = "";
while (some condition)
{
$result = file_get_contents("filename.txt");
$result = "$result\n" . $result;
file_put_contents("filename.txt", $result);
}
echo nl2br ($result);
我想在网页上回显第一行为左对齐,第二行为右对齐,依此类推。有什么办法可以做到这一点?
答案 0 :(得分:0)
$aLines = readfile(<file>);
for( $i = 0; $i < count( $aLines ); $i++ ) {
$align = ($i%2)?"left":"right";
echo "<div style='text-align: {$align}'>{$aLines[$i]}</div>";
}
或者如果您想在文件本身内部对齐:
$aLines = readfile(<source_file>);
$iLineWidth = 100;
$aLinesNew = [];
for( $i = 0; $i < count( $aLines ); $i++ ) {
$sLine = ($i%2)?$aLines[$i]:str_pad( $aLines[$i], $iLineWidth, " ", STR_PAD_LEFT);
$sLine = trim($sLine);
array_push( $aLinesNew, $sLine );
}
file_put_contents( <destination_file>, implode( "\n", $aLinesNew ) );
答案 1 :(得分:0)
首先,您可以使用implode
将文本拆分成购买的数组,并使用CSS对齐它们:
$result = "";
while (some condition){
$result = file_get_contents("filename.txt");
$result = "$result\n" . $result;
file_put_contents("filename.txt", $result);
}
$lines = implode('\n',$result);
echo "<div class='lines'>";
foreach($lines as $line){
echo $line;
}
echo "</div>";
// CSS:
echo '.lines > div:nth(even){ text-align:right; }';