PHP - 使用多行向表格单元格添加换行符

时间:2018-03-31 22:56:30

标签: php html csv html-table newline

我在HTML表格单元格中创建新行时遇到问题。数据来自CSV文件,每个单元格有多行。我尝试使用import random import pygame pygame.init() SIZE = WIDTH, HEIGHT = 800, 600 FPS = 60 class AnimatedWord: def __init__(self, image, position, target, speed=1): self.image = image self.target = image.get_rect().move(*target) self.position = image.get_rect().move(*position) self.speed = speed def update(self): if self.position.y > self.target.y: self.position.y -= self.speed elif self.position.y < self.target.y: self.position.y += self.speed if self.position.x > self.target.x: self.position.x -= self.speed elif self.position.x < self.target.x: self.position.x += self.speed def draw(self, screen): screen.blit(self.image, self.position) def create_word_surfaces(words, font_size=30, color=(106, 90, 205, 0)): font = pygame.font.SysFont("Arial", font_size) surfaces = [] for word in words: surface = font.render(word, True, color) surfaces.append(surface) return surfaces def main(): screen = pygame.display.set_mode(SIZE) background = screen.copy() background.fill((0, 0, 0, 0)) screen.blit(background, (0, 0)) clock = pygame.time.Clock() words = "loading loading loading loading loading loading loading loading loading loading vectors monkey banana reishi argonaut oneironaut purgatory interstitium marmalade savanna chinchilla gobies loading loading leadbetter ".split(" ") targets_x = [i for i in range(0, screen.get_width(), 50)] targets_y = [i for i in range(0, screen.get_height(), 20)] animated_words = [] for surface in create_word_surfaces(words): target_x = random.choice(targets_x) target_y = random.choice(targets_y) animated_word = AnimatedWord(surface, position=(400, 300), target=(target_x, target_y), speed=1) animated_words.append(animated_word) running = True while running: # Main loop clock.tick(FPS) # Limit the framerate to FPS # HANDLE EVENTS for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # UPDATE GAME OBJECTS for x in animated_words: x.update() # DRAW GAME OBJECTS screen.blit(background, (0, 0)) # Fill entire screen. for x in animated_words: x.draw(screen) pygame.display.update() if __name__ == '__main__': main() '\n',但它不起作用。我搜索了论坛但没有找到解决方案。有关如何实现这一目标的任何建议?我应该使用不同的功能吗?感谢..

CSV看起来像这样:

'\r'

我正在使用的代码输出如下文件:

header,header,header,header,header
,,,,"title 
aaaaaaa
bbbbbbb
ccccccc","title 
ddddddd"

我希望它输出如下:

       header                 header
title aaaaaaa bbbbbbb      title ddddddd
ccccccc

我使用的代码来自此主题:Dynamically display a CSV file as an HTML table on a web page

 header    header
 title     title
aaaaaaa   ddddddd
bbbbbbb
ccccccc

我最终使用了nl2br。下面的工作代码。

<?php
echo "<html><body><table>\n\n";
$f = fopen("my.csv", "r");
while (($line = fgetcsv($f, 0)) !== false) {
        echo "<tr>";
        foreach ($line as $cell) {
                echo  "<td>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
?>

1 个答案:

答案 0 :(得分:0)

您可以在php文档之后尝试预定义的常量PHP_EOL,这是“正确的'行尾'”。

事实上,“\ n”是典型的 unix 行尾,大多数窗口都不太了解它。 Php将使用PHP_EOL来处理这个问题。

echo "</tr>".PHP_EOL;

您也可以尝试使用 heredoc (EOF)

http://php.net/manual/en/reserved.constants.php http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc