解析txt文件并将它们转换为静态html文件 [英] Parse txt files and turn them into static html files

查看:75
本文介绍了解析txt文件并将它们转换为静态html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPDATE (11/24/2015)



除了一个小细节之外,我需要弄清楚如何获取我的HTML模板中的静态占位符变量,以便我们可以将它们替换为从TXT文件中提取的内容。



这是我的模板代码:

 <!DOCTYPE html> 
< html>
< head>
< title> {PAGE_TITLE}< / title>
< / head>
< body>
{PAGE_TITLE} - {PAGE_AUTHOR} - {PAGE_DATE}
{PAGE_CONTENT}
< / body>
< / html>






ORIGINAL



我回顾了这个问题 PHP - 解析txt文件,并尽我所能地自己做。



我在PHP中为教育目的创建了一个简单的,非常小的静态站点生成器。我有一个包含所有代码的单个PHP文件的目录(HTML模板除外),它将扫描当前目录中的任何txt文件,并确定是否有多个代码,以便可以使用循环处理每个文件。



我的txt文件结构如下:

  TITLE 
AUTHOR
DATE

文字从这里开始...

我坚持从文件中抽取标题,作者,日期和文本内容的部分,并将它们存储在正确的变量中,以便可以将信息传递给要处理的HTML模板。 / p>

我还想设置它,所以当有换行符/返回时,它会为该块文本附加一个HTML段落标记。



以下是我目前用于PHP文件的代码:

 <?php 
$ files = glob(*。txt); //扫描目录中的.txt文件

//检查目录
中是否有.txt文件if($ files!== false){
$ numberOfFiles = count( $文件); //计算目录

中的.txt文件数目//检查文件数是否大于1
if($ numberOfFiles> 1){
//高级循环会去这里处理多个txt文件
} else {
$ file_handle = fopen($ files [0],r); //打开文件

//逐行循环遍历文件内容
while(!feof($ file_handle)){
$ file = file_get_contents($ files [0 ]); //获取文件内容
$ rows = explode(\\\
,$ file); //计算文件中的行数

//需要从txt文件中抽取TITLE,AUTHOR和DATE
//这里是我需要将文件内容的其余部分解析为段落块的HTML模板

break; //一次运行后断开循环
}

fclose($ file_handle); //关闭文件连接
}
}
?>


解决方案

您可以逐个从文件中获取行,而不是获取整个文件,然后单独格式化它们并将它们放入变量中,以备在页面上回显出来。然而,由Dontfeed代码提出的方法非常优越,效率更高,因此我已经撤回了原文,并希望他会批准我的主意。

$ p> <?php
$ files = glob(*。txt); //扫描目录中的.txt文件

//检查目录
中是否有.txt文件if($ files!== false){
$ numberOfFiles = count( $文件); //计算目录

中的.txt文件数目//检查文件数是否大于1
if($ numberOfFiles> 1){
//高级循环会去这里处理多个txt文件
} else {

$ text_array = array();
$ file_handle = fopen($ files [0],r); //打开文件
$ text_array = stream_get_contents($ file_handle);
$ text_array = explode(\\\
,$ text_array);
//获得前三行
$ page_title = trim($ text_array [0]);
$ all_lines ='< p>'。修剪($ text_array [0])。 ' - '。修剪($ text_array [1])。 ' - '。修剪($ text_array [2])。 < / p为H.;
//删除前四个数组元素
$ text_array [0] = $ text_array [1] = $ text_array [2] = $ text_array [3] ='';
//获取剩余文本
$ text_block = trim(implode($ text_array));
fclose($ file_handle); //关闭文件连接
} // endifs for first if(... statements

?>

HTML输出:

 <!DOCTYPE html> 
< ; html>
< head>
< title><?php echo $ page_title;?>< / title>
< / head>
< body>
<?php echo $ all_lines。\\\
。'< p>。$ text_block。'< / p>'。\\\
;?>
< / body>
< / html>


准备打印到文件的变量:


<? php
$ print_to_file ='<!DOCTYPE html>
< html>
< head>
< title>'。$ page_title。'< / title>
< / head>
< bo dy>'。\\\
。$ all_lines。 \\\
。 '< p>'。 $ text_block。'< / p>'。 \\\

'< / body>
< / html>';

echo $ print_to_file;
?>

HTML在变量中看起来有些偏移,但在打印时出来。



最后,为每行文本添加< p> 标签。

 <?php 
$ files = glob(*。txt); //扫描目录中的.txt文件

//检查目录
中是否有.txt文件if($ files!== false){
$ numberOfFiles = count( $文件); //计算目录

中的.txt文件数目//检查文件数是否大于1
if($ numberOfFiles> 1){
//高级循环会去这里处理多个txt文件
} else {

$ text_array = array();
$ file_handle = fopen($ files [0],r); //打开文件

$ text = stream_get_contents($ file_handle);

//得到前三行
$ text_array = explode(\\\
,$ text);
$ page_title = trim($ text_array [0]);
$ all_lines ='< p>'。 $ text_array [0]。 ' - '。 $ text_array [1]。 ' - '。 $ text_array [2]。 < / p为H.;
//设置一些东西来分割行并添加< p>标记
$ text_array = str_replace(\ n,< / p> \\\
xxx< p>,$ text);
$ text_array = explode(xxx,$ text_array);

//删除前四个数组元素
$ text_array [0] = $ text_array [1] = $ text_array [2] = $ text_array [3] ='';
//获取剩余文本



$ text_block = trim(implode($ text_array));

}
}
?>

此版本可以使用与上面相同的html / php块


UPDATE (11/24/2015)

I have everything working correctly except one small detail. I need to figure out how to get the static placeholder variables I have in my HTML template so that I can replace them with the content pulled from the TXT file.

Here's my template code:

<!DOCTYPE html>
<html>
<head>
  <title>{PAGE_TITLE}</title>
</head>
<body>
  {PAGE_TITLE} - {PAGE_AUTHOR} - {PAGE_DATE}
  {PAGE_CONTENT}
</body>
</html>


ORIGINAL

I reviewed this question PHP - parsing a txt file and got as far as I could on my own.

I'm creating a simple, very small static site generator in PHP for educational purposes. I have a directory with a single PHP file which is where all the code will be (except for the HTML template), and it will scan the current directory for any txt files and decide if there's more than one so a loop can be used to process each file.

My txt file's are structured like so:

TITLE
AUTHOR
DATE

Text starts here...

I'm stuck on the part where I pull the TITLE, AUTHOR, DATE, and text content from the file and store them in their correct variables so that information can be passed to the HTML template to be processed.

I would also like to have it set up so when there is a newline/return it will append a HTML paragraph tag to that block of text.

Here's the code I have so far for the PHP file:

<?php
$files = glob("*.txt"); // Scan directory for .txt files

// Check that there are .txt files in directory
if ($files !== false) {
    $numberOfFiles = count($files); // Count number of .txt files in directory

    // Check if number of files is greater than one
    if ($numberOfFiles > 1) {
        // Advanced loop will go here to process multiple txt files
    } else {
        $file_handle = fopen ($files[0], "r"); // Open file

        // Loop through file contents line-by-line
        while (!feof ($file_handle)) {
            $file = file_get_contents($files[0]); // Get file contents
            $rows = explode ("\n", $file); // Count number of rows in file

            // Need to pull TITLE, AUTHOR, and DATE from txt file
            // Here's where I need the rest of the file's content to be parsed into paragraph blocks for the html template

            break; // Break loop after one run
        }

        fclose ($file_handle); // Close file connection
    }
}
?>

解决方案

You could get the lines from the file one by one, instead of getting the whole file, and then formatting them individually and putting them into variables ready to be echoed out on your page. However, the method proposed by Dontfeedthecode is so far superior and more efficient that I have withdrawn the original and hope that he will approve of what I have done with his idea.

     <?php         
      $files = glob("*.txt"); // Scan directory for .txt files

      // Check that there are .txt files in directory
           if ($files !== false) {
           $numberOfFiles = count($files); // Count number of .txt files in directory

              // Check if number of files is greater than one
              if ($numberOfFiles > 1) {
              // Advanced loop will go here to process multiple txt files
              } else {

              $text_array = array();
              $file_handle = fopen ($files[0], "r"); // Open file
              $text_array = stream_get_contents($file_handle);
              $text_array = explode("\n", $text_array);
              // get the top three lines
              $page_title = trim($text_array[0]);
              $all_lines = '<p>' .  trim($text_array[0]) . ' - ' . trim($text_array[1]) .  ' - ' . trim($text_array[2]) . '</p>';
              // delete the top four array elements
              $text_array[0] = $text_array[1] = $text_array[2] = $text_array[3] = '';
             // get the remaining text
              $text_block =  trim(implode($text_array));
              fclose ($file_handle); // Close file connection
         }  // endifs for first if(... statements
     }
     ?>

HTML Output:

         <!DOCTYPE html>
         <html>
            <head>
               <title><?php echo $page_title; ?></title>
            </head>
                    <body>
                      <?php echo $all_lines . "\n" . '<p>' . $text_block .'</p>'. "\n"; ?>
                    </body>
         </html>


A variable ready to print to file:


         <?php
                   $print_to_file = '<!DOCTYPE html>
               <html>
                     <head>
                           <title>' . $page_title . '</title>
                     </head>
                       <body>' . "\n"  . $all_lines . "\n" . '<p>' . $text_block .'</p>'. "\n" .
                       '     </body>
          </html>';

         echo $print_to_file;
         ?>

HTML looks a bit displaced in the variable here but comes out right when printed.

And finally, a version which puts a <p> tag for each line of the text.

     <?php
     $files = glob("*.txt"); // Scan directory for .txt files

    // Check that there are .txt files in directory
     if ($files !== false) {
     $numberOfFiles = count($files); // Count number of .txt files in directory

         // Check if number of files is greater than one
         if ($numberOfFiles > 1) {
         // Advanced loop will go here to process multiple txt files
         } else {

         $text_array = array();
         $file_handle = fopen ($files[0], "r"); // Open file

         $text = stream_get_contents($file_handle);

         // get the top three lines
         $text_array = explode("\n", $text);
         $page_title = trim($text_array[0]);
         $all_lines = '<p>' .  $text_array[0] . ' - ' . $text_array[1] .  ' - ' . $text_array[2] . '</p>';
         // set up something to split the lines by and add the <p> tags
         $text_array = str_replace("\n","</p>\nxxx<p>", $text);
         $text_array = explode("xxx", $text_array);

         // delete the top four array elements
         $text_array[0] = $text_array[1] = $text_array[2] = $text_array[3] = '';
         // get the remaining text



         $text_block =  trim(implode($text_array));

         }
     }
     ?>

This version can use the same html/php blocks as above

这篇关于解析txt文件并将它们转换为静态html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆