【问题标题】:How to generate in PHP a series of HTML pages based on CSV data?如何在 PHP 中生成一系列基于 CSV 数据的 HTML 页面?
【发布时间】:2020-09-22 18:59:47
【问题描述】:

一个问题:需要在 PHP 脚本中生成一系列 HTML 页面,并从 CSV 中的每一行数据(TITLE、DESCRIPTION、H1、IMG、Text、A HREF = "URL")中输出数据。

$image_number
$image_name
$title
$description
$image_source
$keywords
$url
$page_name

也就是说,实际上CSV中的每一行,当用PHP处理时>,根据给定的单个模板,将页面提供给画廊> HTML或PHP目录,即每个单独的物理页面($指定地址的页面名称在服务器上) 与来自 CSV 文件的数据的这个字符串的每个变量的值:$title,$description,$h1,$image_source,$keywords,$image(number),$url=...

这对于在 Google 图片中建立索引是必要的,但与 MySQL 通信以处理查询还没有意义。

CSV 目录:https://drive.google.com/file/d/1F_zWVaFDtjp82NDMZdvhWHyB1GYq4BmG/view?usp=sharing

用于处理 CSV 和卸载模板页面的示例 PHP 代码 *:(* 据我了解 - 并且写过,可能有错误)。

<?php
    function CSVtoHTML {
        $row = 1;
if (($handle = fopen ("catalog.csv", "r"))! == FALSE) {
  while (($data = fgetcsv ($handle, 30, ","))! == FALSE) {
    for each ($row[i]) {
                $column[1] = $image_number;
                $column[2] = $image_name;
                $column[3] = $title;
                $column[4] = $description;
                $column[5] = $image_source;
                $column[6] = $url;
                $column[7] = $page_name;
                $content = '<! doctype html>
                <html lang="en">
                    <head>
                        <meta charset = "UTF-8">
                        <title>'.$title.'</title>
                        <meta description = "'.$ description.'" />
                    </head>
                    <body>
                        <div class="content">
                            <h1> '.$h1.'</h1>
                            <img src = "'.$image_source.'" alt = "">
                            <h2> '.$image_name.' </h2>
                            <p> Keywords: '.$keywords.' </p>
                            <p> <a href="/register?value='.$image_number.'"> Buy now </a> </p>
                        </div>
                    </body>
                </html> ';
/ * How to write data in HTML with UTF-8 encoding to a folder on the server with a given name? * /
                $htmldata = file_get_contents ($content);
                $htmldata = mb_convert_encoding ($htmldata, 'UTF-8');
                file_put_contents ('graphics/'.$page_name.'.html', $htmldata);
           }
           fclose ($handle);
       }
       return $data;
       echo "Catalog generated now"
    }
    else {
        echo "Catalog in CSV is empty or broken."
    }
}
?>

CSV 中的每一行,当在 PHP 中处理时,根据给定的单个模板将页面输出到 HTML 目录中的图形,即每个单独的物理页面(名称 $ page_name 在服务器上的指定地址)带有来自 CSV 的数据给定字符串的文件,每个变量的值:$title, $description, $h1, $image_source, $keywords, $image(number), $url = ...

这对于在 Google 图片中建立索引是必要的,但与 MySQL 通信以处理查询还没有意义。

【问题讨论】:

    标签: php html csv


    【解决方案1】:

    你最好做一些动态的东西而不是创建 .html 文件而是回答。

    第一行是标题,将其存储在一个变量中,然后将其与array_combine 一起使用以创建您的关联数组,尽管您可以通过索引访问。

    <?php
    function CSVtoHTML() {
        $row = 0;
        $header = [];
        if (($handle = fopen("catalog.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    
                // first line is header, grab it then continue
                if ( $row === 0) {
                    $header = $data;
                    $row++;
                    continue;
                }
    
                // make associative array from header and data
                $data = array_combine($header, $data);
    
                // make document (you need to work on this, but your see var is accessed by header name)
                $content = '<!doctype html>
                    <html lang="en">
                        <head>
                            <meta charset = "UTF-8">
                            <title>'.$data['Title'].'</title>
                            <meta description = "'.$data['Description'].'" />
                        </head>
                        <body>
                            <div class="content">
                                <h1> '.$data['Title'].'</h1>
                                <img src="'.$data['Image Source'].'" alt>
                                <h2> '.$data['Image Name'].' </h2>
                                <p> Keywords: '.$data['Keywords'].' </p>
                                <p> <a href="/register?value='.$data['Image Number'].'"> Buy now </a> </p>
                            </div>
                        </body>
                    </html> ';
    
                // make the file
                file_put_contents('./graphics/'.$data['Page Name'], $content);
    
                $row++;
            }
            fclose($handle);
        }
    }
    
    // and don't forget to call it
    CSVtoHTML();
    

    【讨论】:

    • 我现在测试了它。它不会将页面保存在 html 中:[link]bundlespace.com/script-parser,base:[link]bundlespace.com/imagebase.csv
    • 有效 > 只添加:$filedata = file_get_contents($content); $filedata = mb_convert_encoding($content, 'UTF-8'); file_put_contents(''.$data['页面名称'], $filedata);
    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多