从文件中读取Q& A的对吗?

时间:2013-12-19 20:44:09

标签: php text file-io

我希望创建一个充满问题和单个答案的简单文本文件。基本布局思路是:

[Q] Question 1
[A] Answer 1
[-]
[Q] Question 2
[A] Answer 2
[-]

每个答案后都是分隔符[ - ]表示Q / A组合完成。答案可以在多行上运行和/或包含一些基本的HTML。

例如:

[Q] What is PHP ?
  [A] <b>PHP</b> is a server-side scripting language designed for web development but also used as a general-purpose programming
 language.<br/>PHP is now installed on more than 244 million websites
 and 2.1 million web servers.<a href='#'>[2]</a><br/>Originally created
 by Rasmus Lerdorf in 1995, the reference implementation of PHP is now
 produced by The PHP Group
  [-]
  [Q] Question 2
  [A] Answer 2
  [-]

文本文件将手写并存储在本地服务器上。因此,如果需要,可以更改布局。但我确实希望能够在格式化中使用一些HTML。

我想要尝试做的是将此文件读入php页面并循环遍历每个Q / A对并在屏幕上显示。

问题和答案的输出类似于:

<a href="#" class="question">QUESTION</a>
<div class="answer">
<strong>[Q] QUESTION</strong><br/>
[A]ANSWER
</div>

有人能指出我正确的方向来阅读这些对,然后输出它们。

谢谢:)

注意:Q1的示例答案取自维基百科;)

更新 这是我到目前为止所得到的: 问题:

[Q] Question 1
[A] Answer 1
[-]
[Q] Question 2
[A] Answer 2
[-]
[Q] Question 3
[A] Answer 3
[-]
[Q] Question 4
[A] <b>PHP</b> is a server-side scripting language designed for web development but also used as a general-purpose programming language.<br/>PHP is now installed on more than 244 million websites and 2.1 million web servers.<a href='#'>[2]</a><br/>Originally created by Rasmus Lerdorf in 1995, the reference implementation of PHP is now produced by The PHP Group
[-]
[Q] Question 5
[A] Answer 5
[-]
[Q] Question 6
[A] Answer 6
[-]

脚本:

<?php
$handle = fopen("questions.txt","r");

if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if (substr($line,0,3)==="[Q]") {
            echo "<a href='#' class='question'>$line</a>";
            $question = $line;
        }

        if (substr($line,0,3)==="[A]") {
            echo "<div class='answer'>";
            echo "<strong>$question</strong><br/>";
            echo "$line</div>";
        }

        if (substr($line,0,3)==="[-]") {
        echo "<br/>";
        continue;
        }

    }
} else {
    echo "Unable to open file";
}

?>

这有效,但它要求每个问题都是一行。理想情况下,我想确保它们匹配Q / A对,答案可以分为多行。

1 个答案:

答案 0 :(得分:3)

好的,我们假设[Q][A][-]标记始终位于一行的开头。

我设置了一个变量,该变量保存在内存中,其中part解析php脚本的文件,然后读取该行。这很简单。另外,我以不同的方式管理$question。这应该可以解决问题。

<?php
$handle = fopen("questions.txt","r");
$part = 0;
$question = "";
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if (strlen($line) > 2) {
            if (substr($line,0,3)==="[Q]") {
                $part = 0;
                echo "<a href='#' class='question'>".$line;
                $question = $line;
            }

            else if (substr($line,0,3)==="[A]") {
                $part = 1;
                echo "</a><div class='answer'>";
                echo "<strong>".$question."</strong><br/>";
                $line = substr($line, 3, strlen($line)-3);
                echo $line;
            }

            else if (substr($line,0,3)==="[-]") {
                echo "</div><br/>";
                continue;
            }

            else {
                echo $line;
                if ($part == 0)
                    $question .= $line;
            }
        }
        else {
            echo $line;
            if ($part == 0)
                $question .= $line;
        }
    }
}
else {
    echo "Unable to open file";
}
?>