0 Replies - 156 Views - Last Post: 03 May 2013 - 02:47 AM Rate Topic: -----

#1 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Looping with the Regex

Posted 03 May 2013 - 02:47 AM

Having a text file, I want to be able to loop through a regex, such that it diplays one question and answers at a time upon submission it takes me to the next question upto the last one. These are the codes that have the regex:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
	style1 {font-family: "Baskerville Old Face"}
.style2 {
	font-family: "Baskerville Old Face";
	font-size: 12px;
	font-style: italic;
body {
	background-color: #99CC66;
    <title>Question and Answer Read From Text File</title>
	<h3> Current Date and Time</h3>
	<script language="javascript">
today = new Date();
document.write("The time now is: ", today.getHours(),":",today.getMinutes());
document.write("<BR>The date is: ", today.getDate(),"/",today.getMonth()+1,"/",today.getYear());
        <form action="processor.php" method="POST">
        <b><u> QUESTIONS AND ANSWERS QUIZ</u></b> <br /><br />
$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
$string = fread($openFile, filesize("questionandanswers.txt"));
//Loop through each line of every question

//Regex to get from the first number to the string's end, so we ignore the Number Question... bit;
preg_match('/\d.*/', $string, $match);

//We get all the strings starting with a number until it finds another number (which will be the beginning of another question;
preg_match_all('/\d\D*/', $match[0], $results);

$qas = array(); // We prepare an array that will store the questions/answers
foreach($results[0] as $result)
    //Separating the question from the string with all the answers.
    list($question, $all_answers) = explode(':', $result);

    //Separating the different answers
    $answers_array = explode('.', $all_answers);
	//removes unnecesary spaces in the questions
    $answers_array_trimmed = array_map('trim', $answers_array);
    //Storing the question and the array with all the answers into the previously prepared array;
    $qas[] = array('question' => $question, 'answers' => $answers_array);
//Looping through the array and outputting all the info into a form;
$questionNr = 0;    
foreach($qas as $k=>$v)
    echo "<input id='question{$questionNr}' style='width:40%' type='text' value='".$v['question']."' name='questions[]' >";
    echo '<br /><br />';
//Puts the answers of each question unique and in the form of radios
    foreach($v['answers'] as $answer)
        echo "<input type=\"radio\" name=\"selected_answers[$questionNr]\" value=\"$answer\" />";
        echo "$answer";
        echo '<br/><br />';
<input name= "submit" type="submit" value="Submit Answers">&nbsp <input type="reset" value ="Clear Answers">

Is This A Good Question/Topic? 0
  • +

Page 1 of 1