简体   繁体   English

如何使用php从文本文件显示定义列表

[英]How to display definition list from text file using php

I have a definition list of movie credits in a text file. 我在一个文本文件中有电影学分的定义列表。 I used a preg_split to read and separate the file with the following line: 我使用preg_split读取并通过以下行分隔文件:

$infoLines= preg_split( "/\\n|:/", file_get_contents(getPath($dir,"info.txt")));

I need to display the contents of this file with each even numbered index will have a <dt> tag while every odd numbered index will have a <dd> tag. 我需要显示此文件的内容,每个偶数索引将具有<dt>标记,而每个奇数索引将具有<dd>标记。 I am trying to do this by using a foreach loop. 我正在尝试通过使用foreach循环来做到这一点。

I found a tutorial site with the following example 我找到了一个带有以下示例的教程站点

$movie = array( "title" => "Rear Window",
            "director" => "Alfred Hitchcock",
            "year" => 1954,
            "minutes" => 112 );

echo "<dl>";

foreach ( $movie as $key => $value ) {
echo "<dt>$key:</dt>";
echo "<dd>$value</dd>";
}

echo "</dl>";

Which prints: 哪些打印:

title:
    Rear Window
director:
    Alfred Hitchcock
year:
    1954
minutes:
    112

I tried to incorporate something like this into my code and I couldn't get it to work since I am reading from a file and not setting the array/key manually. 我试图将类似的内容合并到我的代码中,但是由于我正在从文件中读取并且未手动设置数组/键,因此无法正常工作。

function DisplayInfo($dir){
  $infoLines= preg_split( "/\n|:/", file_get_contents(getPath($dir, "info.txt")) );


  echo "<dl>";
  foreach ($infoLines as $key => $value) {
     echo"<dt>$key</dt>";
     echo"<dd>$value</dd>";
  }
  echo "</dl>";
}

My output is: 我的输出是:

0
    STARRING
1
    Cary Elwes, Robin Wright, Andre the Giant, Mandy Patinkin
2
    DIRECTOR
3
    Rob Reiner

But I need: 但是我需要:

STARRING
    Cary Elwes, Robin Wright, Andre the Giant, Mandy Patinkin

DIRECTOR
    Rob Reiner

I feel like I am almost there and have just hit a dead end. 我觉得自己快要死了,已经走到了尽头。 How can I properly display definition lists in PHP? 如何在PHP中正确显示定义列表?

There are plenty of examples such as the one I posted but none dealing with information from txt files. 有很多示例,例如我发布的示例,但没有一个示例处理txt文件中的信息。

Based on your code I'm assuming a line in your file looks something like 根据您的代码,我假设文件中的一行看起来像

DIRECTOR:Rob Reiner

In which case, you shouldn't be splitting the line data until you are ready to print. 在这种情况下,在准备打印之前,请勿拆分行数据。

function DisplayInfo($dir){
  $infoLines= file(getPath($dir, "info.txt"));
  echo "<dl>";
  foreach ($infoLines as $line) {
     list($key, $value) = explode(':', $line, 2); 
     echo"<dt>$key</dt>";
     echo"<dd>$value</dd>";
  }
  echo "</dl>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Ajax从下拉列表中使用JQuery显示.php文件? - How to display a .php file with Ajax using JQuery from a dropdown list? "PHP - 使用文本文件中的名称集显示目录" - PHP - display directories using set of names from text file 使用JavaScript从URL提取文本并显示在php文件上 - Extract text from URL using javascript and display on php file 如何使用PHP SDK v3从Amazon S3显示文本文件的内容 - How to display contents of text file from Amazon S3 using PHP SDK v3 如何使用带有 Azure 存储帐户的 Azure 文件中的 PHP 显示文本内容本身? - How can display text content itself using PHP from Azure File with Azure storage account? 使用PHP如何读取多个TEXT文件并显示文件名? - using php how to read multiple TEXT files and display the file name? 调用并显示来自php文件的文本文件 - call and display a text file from a php file 如何根据PHP中的持续时间从文件中随机显示文本 - How to display text randomly from a file based on a duration of time in php 如何使用PHP在列表中显示 - How To Display In A List Using PHP 如何使用php在html文本框中显示数据库中的数据 - how to display data from database in html text boxes using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM