MacMan
MacMan

Reputation: 933

Read Pairs of Q&A's from file?

I'm looking to create a simple text file full of question and single answers. The basic layout idea is :

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

After each answer is a separater [-] to indicate that Q/A combination is complete. The answer could run over multiple lines and/or include some basic HTML.

Eg:

[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
  [-]

The text file will be written by hand and stored on a local server. So the layout can be changed if needed. But I do want to be able to use some HTML in the formatting.

What I want to try an do is read this file into a php page and loop through each Q / A pair and present them on screen.

The Question and Answer will be outputted similar to :

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

Can someone point me in the right direction for reading the pairs and then outputting them.

Thanks :)

note: the example answer to Q1 is taken from Wikipedia ;)

UPDATE: This is what I've got so far: Questions:

[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
[-]

Script:

<?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";
}

?>

This works but it requires each question to be a single line. Ideally I'd like to ensure they are matching Q/A Pairs and the answer can be split over multiple lines.

Upvotes: 1

Views: 93

Answers (1)

Vereos
Vereos

Reputation: 513

Ok, let's assume that [Q], [A] and [-] tags will always be at the beginning of a line.

I'd set up a variable that keeps in memory in which part of the file parsing the php script is, and then reads the line. It is quite simple. Also, I managed $question in a different way. This should do the trick.

<?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";
}
?>

Upvotes: 3

Related Questions