在php中将数据CSV转换为SVG所有元素

时间:2016-09-07 09:31:01

标签: php csv svg

我有这个代码。

 <?php
$csv = "
4841625010111,Marimea,42,2,350,,C221980;00902584944032461614@\n
4841625010112,Marimea,44,2,350,,C221980;00902584944032560098@\n
4841625010113,Marimea,46,2,350,,C221980;00902584944032506812@\n
4841625010114,Marimea,48,2,350,,C221980;009025849440325421:0@\n
4841625010115,Marimea,50,2,350,,C221980;00902584944032164230@\n
4841625010116,Marimea,52,2,350,,C221980;00902584944032524<41@\n
4841625010117,Marimea,54,2,350,,C221980;0090258494403221250<@\n

";
$csv_arr = explode("\n", $csv);

echo "<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n";
echo " width=\"252px\" height=\"144px\" viewBox=\"0 0 252 144\" enable-background=\"new 0 0 252 144\" xml:space=\"preserve\">\n";

foreach ($csv_arr as $card) {
    $row = explode(",", $card);
     echo "<text display=\"block\">\n";
     echo "Size<tspan x=\"0\" y=\"0\" font-family=\"'Arial\" font-size=\"12\">{$row[2]}</tspan>\n";
     echo "Pret<tspan x=\"0\" y=\"14.4\" font-family=\"'Arial'\" font-size=\"12\">{$row[4]}</tspan>\n";
     echo "<tspan x=\"0\" y=\"28.8\" font-family=\"'Arial'\" font-size=\"12\">{$row[2]}</tspan>\n";
     echo "<tspan x=\"0\" y=\"43.2\" font-family=\"'Barcode'\" font-size=\"12\">{$row[6]}</tspan></text>\n";
     echo "<tspan x=\"0\" y=\"43.2\" font-family=\"'Arial'\" font-size=\"12\">{$row[3]}</tspan></text>\n"; 
     echo "<\br><\br><\br><\br>";
}
echo "</svg>";
 ?>

并且在结果中我将所有部件都内联。但我需要换行。在这里看到结果

http://tt.portavita.md/test.php

请帮助解决此问题

1 个答案:

答案 0 :(得分:0)

SVG不接受新行的\ n或中断标记,您需要设置高度(y坐标)以使值显示在新行中。你似乎也在错误的地方关门。你的代码应该看起来像这样。

<?php
$csv = "
4841625010111,Marimea,42,2,350,,C221980;00902584944032461614@\n
4841625010112,Marimea,44,2,350,,C221980;00902584944032560098@\n
4841625010113,Marimea,46,2,350,,C221980;00902584944032506812@\n
4841625010114,Marimea,48,2,350,,C221980;009025849440325421:0@\n
4841625010115,Marimea,50,2,350,,C221980;00902584944032164230@\n
4841625010116,Marimea,52,2,350,,C221980;00902584944032524<41@\n
4841625010117,Marimea,54,2,350,,C221980;0090258494403221250<@\n

";
$csv_arr = explode("\n", $csv);

echo "<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n";
echo " width=\"252px\" height=\"144px\" viewBox=\"0 0 252 144\" enable-background=\"new 0 0 252 144\" xml:space=\"preserve\">\n";
$y_height=14;
foreach ($csv_arr as $card) {
    $row = explode(",", $card);
    echo "<text display=\"block\" x=\"0\" y=\"{$y_height}\">";
    echo "<tspan x=\"0\" y=\"{$y_height}\" font-family=\"'Arial\" font-size=\"12\">Size {$row[2]}</tspan>";
    $y_height += 14;
    echo "<tspan x=\"0\" y=\"{$y_height}\" font-family=\"'Arial'\" font-size=\"12\">Pret {$row[4]}</tspan>";
    $y_height += 14;
    echo "<tspan x=\"0\" y=\"{$y_height}\" font-family=\"'Arial'\" font-size=\"12\">{$row[2]}</tspan>";
    $y_height += 14;
    echo "<tspan x=\"0\" y=\"{$y_height}\" font-family=\"'Barcode'\" font-size=\"12\">{$row[6]}</tspan>";
    $y_height += 14;
    echo "<tspan x=\"0\" y=\"{$y_height}\" font-family=\"'Arial'\" font-size=\"12\">{$row[3]}</tspan>"; 
    $y_height += 14;
    echo "</text>";
}
echo "</svg>";
?>

DEMO