Line breaking in PHP with CSV

Asked

Viewed 972 times

1

Hello, my friends!

I have this CSV : inserir a descrição da imagem aqui And this code in PHP:

<?php
// Create a table from a csv file 
$area = 0;
echo "<table class=\"table table-hover\">\n\n";
$f = fopen("horarios_modificados.csv", "r", ";");
while (($line = fgetcsv($f)) !== false) {
    if($area == 0) echo "<thead>\n";
    if($area == 1) echo "<tbody>\n";
    $row = $line[0];    // We need to get the actual row (it is the first element in a 1-element array)
    $cells = explode(";",$row);
    echo "<tr>\n";
    foreach ($cells as $cell) {
        echo "<td>" . htmlspecialchars($cell) . "</td>\n";
    }
    echo "</tr>\n";
    if($area == 0) echo "</thead>\n";
    $area++;
}
fclose($f);
echo "</tbody>\n";
echo "\n</table>\n";
?>

I would like the exclamation point "!" (already inserted in my CSV) is a code in PHP to give new line (line break) in CSV when it’s in php in browser.

'Cause these days it’s like this: inserir a descrição da imagem aqui

Could you help me?

1 answer

3


Looking quickly, I think this would suffice:

echo "<td>" . str_replace("!", "<br>", htmlspecialchars($cell)) . "</td>\n";
  • It worked! I’ve tried it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.