Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You <code>explode()</code> using space as separator, that's why you are getting each word as an array element. <code>explode()</code> just uses the character you give it and split the string whenever it encounters the character.</p> <p>Your data (the file) hasn't got a pattern. So you need to stablish some rules in the text, or you won't be able to separate the information you want:</p> <p>I modified your text stablishing some rules:</p> <ol> <li>The questions will be finished with a colon(:).</li> <li>The answers will be finished with a dot(.), all of them but the last one, as we will use the number of the question as separator.</li> <li>The questions or answers won't contain any numbers [0-9] or it will confuse the regex.</li> </ol> <p>This is the resulting text I modified manually to make the text work:</p> <pre><code>$string = 'Number Question (a) (b) (c) (d) 1 The most important feature of spiral model is: requirement analysis. risk management. quality management. configuration management 2 The worst type of coupling is: Data coupling. control coupling. stamp coupling. content coupling 3 One of the fault base testing techniques is: unit testing. beta testing. Stress testing. mutation testing 4 A fault simulation testing technique is: Mutation testing. Stress testing. Black box testing. White box testing 5 RS is also known as: specification of White box testing. Stress testing. Integrated testing. Black box testing'; </code></pre> <hr> <p>The solution: After that we can use some code to separate the information:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;read&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;b&gt;&lt;u&gt; QUESTIONS AND ANSWERS QUIZ&lt;/u&gt;&lt;/b&gt; &lt;br /&gt; &lt;?php $openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text file"); $string = fread($openFile, filesize("questionandanswers.txt")); //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 with all the questions/answers foreach($results[0] as $result){ //Separating the answer from the string with all the answers. list($question, $all_answers) = explode(':', $result); //Separating the different answers $answers_array = explode('.', $all_answers); //Stuffing the question and the array with all the answers into the previously prepared array; $qas[] = array('question' =&gt; $question, 'answers' =&gt; $answers_array); } //Looping through the array and outputting all the info into a form; foreach($qas as $k =&gt; $v){ echo "&lt;label&gt;{$v['question']}&lt;/label&gt;&lt;br/&gt;"; echo "&lt;select&gt;"; //we loop through $v['answers'] because its an array within the array with all the answers. foreach($v['answers'] as $answer){ echo "&lt;option&gt;$answer&lt;/option&gt;";//the output } echo "&lt;/select&gt;"; echo "&lt;br/&gt;&lt;br/&gt;"; } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Looks complex because of all the comments, they are less than 20 lines of text actually</p> <p>You can see the output here: <a href="http://aleation.com/test3.php" rel="nofollow">output</a></p> <hr> <p><strong>Notes</strong> Did this just to practice, but next time try to research more, and ask specific questions, or people will ignore/downvote your, have a good read about the Stackoverflow's FAQs</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload