首页 > 解决方案 > CSV 过滤列和行并回显为 XML - - 我有工作代码来回显 HTML 表

问题描述

我发现许多线程将 CSV 文件回显为 XML,但无法弄清楚如何过滤行和列。

下面的代码正是我想要的,但输出是一个 HTML 表。

我需要帮助来为 xml 输出创建类似的代码。

场景 站点地图.csv

1,https://example.com/page1,monthly,1
7,https://example.com/page2,monthly,0.5
14,https://example.com/page3,monthly,0.5
21,https://example.com/page4,monthly,0.5

这是一个静态网站,所有页面都已创建,但应仅以每周 1 次的速度出现在站点地图中,即 2020 年 2 月 9 日 $launchdate 之后的 10 天,站点地图中仅应显示前两行。

不应回显第一列 - 它用于确定是否应在加载时回显该行。

我查看了许多将 csv 转换为 XML 的脚本,但它们都显示了所有列。我已经尝试了几个小时来提出一个以 XML 格式创建结果的解决方案。

我知道最终解决方案需要包括以下内容:

header("Content-Type: application/xml; charset=utf-8"); 
    echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' .PHP_EOL; 

我知道最终解决方案将需要排除顶部日期数据的回声 - 目前用于测试

我知道最终解决方案将需要替换列描述符

eg Loc will become <loc> URL </loc>  then <lastmod> <changefreq>  <priority>

======

Working code to echo HTML table
<?php
//Sources for this solution
// https://thisinterestsme.com/reading-csv-file-with-php/
// and &  https://stackoverflow.com/questions/40285479/filtering-and-displaying-a-csv-file-with-php 

// Final solution should be xml not HTML table

$date = date("Y-m-d");  // Todays Date
$date_number = strtotime($date); // Todays date as Integer
$launchdate_number = strtotime('2020-02-09'); //Launch Date as Integer 1581206400
$days_since_launch = ($date_number - $launchdate_number) / 86400; // Days since Launch date

// Echo data for testing, will not be in the final XML file
echo "date ";
echo $date;
echo " ";
echo "date_number ";
echo $date_number;
echo " ";
echo "launchdate ";
echo $launchdate_number;
echo " ";
echo "days_since_launch ";
echo " ";
echo $days_since_launch;
echo "<br>";
$fileHandle = fopen("sitemap.csv", "r");

//Loop through the CSV rows.
while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) {

    if ($row[0] > $days_since_launch) continue; // display only pages were value in 1st column[0]less that $days_since_launch
    //Print out my column data - exclude 1st column[0]
    echo 'Loc: ' . $row[1] . '<br>';
    echo 'Lastmod: ' . $date . '<br>';
    echo 'changefreq: ' . $row[2] . '<br>';
    echo 'priority: ' . $row[3] . '<br>';
    echo '<br>';
}
       fclose($file_handle);
?> 

标签: phpxmlcsv

解决方案


几个小时后,我设法找到了一个解决方案,它可以读取 CSV 文件并回显有效的 xml 站点地图。感谢代码注释中的来源。

<?php   
//Sources for this solution
    // https://thisinterestsme.com/reading-csv-file-with-php/
    // https://stackoverflow.com/questions/40285479/filtering-and-displaying-a-csv-file-with-php 
    // https://www.webslesson.info/2017/06/make-dynamic-xml-sitemap-in-php-script.html

    header("Content-Type: application/xml; charset=utf-8");

    echo '<?xml version="1.0" encoding="UTF-8"?>'; 

    $date = date("Y-m-d");  // Todays Date
    $date_number = strtotime($date); // Todays date as Integer
    $launchdate_number = strtotime('2020-02-09'); //Launch Date as Integer 1581206400
    $days_since_launch = ($date_number - $launchdate_number) / 86400; // Days since Launch date

    // Echo data for testing, will not be in the final XML file


    $fileHandle = fopen("sitemap.csv", "r");
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
    //Loop through the CSV rows.
    while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) {

        if ($row[0] > $days_since_launch) continue; // display only pages were value in 1st column[0]less that $days_since_launch
        //Print out my column data - exclude 1st column[0]
     echo '<url>' . PHP_EOL;
     echo '<loc>'. $row[1] .'</loc>' . PHP_EOL;
     echo '<lastmod>'.$date.'</lastmod>' . PHP_EOL;
     echo '<changefreq>'. $row[3] .'</changefreq>' . PHP_EOL;
     echo '<priority>'. $row[4] .'</priority>' . PHP_EOL;
     echo '</url>' . PHP_EOL;  

    }
    echo '</urlset>';
    ?>

推荐阅读