haku87 Posted April 5, 2007 Share Posted April 5, 2007 Hi, I am currently working on an elearning portal.. Currently, I looking at way to import the quiz question into database. I found one format quite useful. However, I met problem in extracting out the information. Example : 1. The Pool Manager in Blackboard is a. A high-schooler on their summer vacation b. Paul Newman hustling Tom Cruise *c. A mechanism for creating quizzes in Blackboard I could use explode to extract out the question but however, how do i know that it is the end of question and start of answer. * meaning is the answer. How am I going to extract this information out such that i only get the answer only. http://www.wcc.vccs.edu/services/blackboard/help.html Quote Link to comment Share on other sites More sharing options...
Barand Posted April 5, 2007 Share Posted April 5, 2007 try <?php $txt = "1. The Pool Manager in Blackboard is a. A high-schooler on their summer vacation b. Paul Newman hustling Tom Cruise *c. A mechanism for creating quizzes in Blackboard "; $qend = strpos($txt, 'a. '); $question = substr($txt, 0, $qend); $apos = strpos($txt, '*'); $aend = strpos($txt, "\n", $apos); $answer = substr($txt, $apos, $aend-$apos); echo "$question<br>$answer"; ?> -->[pre] 1. The Pool Manager in Blackboard is *c. A mechanism for creating quizzes in Blackboard [/pre] Quote Link to comment Share on other sites More sharing options...
haku87 Posted April 5, 2007 Author Share Posted April 5, 2007 Erm.. It is working.. But how am i going to make it dynamic such that. It will check for "a. ", "b. " until there is no more option left and able to return me the number of option the user key in Thanks in advance. Thanks very much Quote Link to comment Share on other sites More sharing options...
Barand Posted April 5, 2007 Share Posted April 5, 2007 That's not exectly the question you originally posted. There should be enough clues in the above code about extracting portions of the text. Quote Link to comment Share on other sites More sharing options...
haku87 Posted April 5, 2007 Author Share Posted April 5, 2007 Ya, But my problem now lie in how do i determine the end of the code I was thinking of this. $compare = array('a. ','b. ','c. '); for($x=0;$x<???;$x++){ $opos = strpos($question,$compare[$x]); $oend = strpos($question,"\n",$opos); $option[$x] = substr($question, $opos, $oend-$opos); /*"if($option == null) break;"*/ } But let assume we did not know how many option it has. Maybe it may have 'g. '. Is there a way to terminate it? I was thinking of inserting "if($option == null) break;" Quote Link to comment Share on other sites More sharing options...
effigy Posted April 5, 2007 Share Posted April 5, 2007 <pre> <?php $data = <<<DATA 1. The Pool Manager in Blackboard is a. A high-schooler on their summer vacation b. Paul Newman hustling Tom Cruise *c. A mechanism for creating quizzes in Blackboard 2. PHP is *a. A language b. A fruit c. A car 3. Regular Expressions are a. A body language *b. Awesome c. Mathematical units DATA; preg_match_all('/^\s*\d+\.\s*([^\r\n]+).*?\*[a-z]\.\s*([^\r\n]+)/sm', $data, $matches, PREG_SET_ORDER); // Toss the full matches. foreach ($matches as &$match) { array_shift($match); } print_r($matches); ?> </pre> Quote Link to comment Share on other sites More sharing options...
haku87 Posted April 5, 2007 Author Share Posted April 5, 2007 Sorry, I have problem understanding ur code. Could u able to do some explaination? Ur help is greatl appreciated? Why need a <pre ></pre >? What does it do. Especially this line. '/^\s*\d+\.\s*([^\r\n]+).*?\*[a-z]\.\s*([^\r\n]+)/sm'; regex pattern? But how u specify there? Thanks for all ur help Quote Link to comment Share on other sites More sharing options...
effigy Posted April 5, 2007 Share Posted April 5, 2007 The pre tag is explained here--it's great for examples, but rarely used for production code. Have a look at the Regular Expressions link in my signature and let me know what you have difficulty with. Quote Link to comment Share on other sites More sharing options...
haku87 Posted April 5, 2007 Author Share Posted April 5, 2007 Hi, Sorry. Could you tell me what pattern '/^\s*\d+\.\s*([^\r\n]+).*?\*[a-z]\.\s*([^\r\n]+)/sm' is matching. Meaning it is looking for wat pattern. There isn't any number in the pattern. i assume that [a-z] is the part that look for 'a. ','b. '.? ? Presumming, + is to match the condition. + is to match the condition. why is there a condition ? there? Quote Link to comment Share on other sites More sharing options...
effigy Posted April 5, 2007 Share Posted April 5, 2007 ^ ### Matches BOL (Beginning of Line) or after a new line since the "m" modifier is used. \s* ### 0 or more instances of whitespace. \d+ ### 1 or more instances of a digit. \. ### A period. \s* ([^\r\n]+) ### Capture 1 or more instances of everything but a CR or LF. .*? ### 0 or more instances (ungreedy) of any character, including a new line since the "s" modifier is used. \* ### An asterisk. [a-z] ### 1 instance of any character from a to z. \. \s* ([^\r\n]+) Quote Link to comment Share on other sites More sharing options...
haku87 Posted April 5, 2007 Author Share Posted April 5, 2007 Hi, thanks. Manage to figure some out. The pattern from what u say is trying to match a pattern. new line follow by : [0-9] number follow by [.] . follow by whitespace. follow by anything expect a new line. follow by a character. on and on But, it has not solved my original problem. It only take out the correct answer. How am I going to take out all the option and put them into array. Erm.. Using this regular expression, '/[a-z]\.([^\r\n]+)' it manage to take out all option. However, how do i put all option inside a array so that it is for one question only. Eg. array[0][0] is the first option for question 1 array[1][1] is the second option for question 2. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 5, 2007 Share Posted April 5, 2007 <pre> <?php $data = <<<DATA 1. The Pool Manager in Blackboard is a. A high-schooler on their summer vacation b. Paul Newman hustling Tom Cruise *c. A mechanism for creating quizzes in Blackboard 2. PHP is *a. A language b. A fruit c. A car 3. Regular Expressions are a. A body language *b. Awesome c. Mathematical units DATA; // Break the string into question groups. $questions = preg_split('/\s+(?=\d+\.)/', $data, -1, PREG_SPLIT_NO_EMPTY); // Loop... $i = 0; foreach ($questions as $question_group) { // Get question. preg_match('/^\d+\.\s+([^\r\n]+)/', $question_group, $matches); $question = $matches[1]; // Get answers. preg_match_all('/^\s+\*?[a-z]\.\s+([^\r\n]+)/sm', $question_group, $answers); array_shift($answers); // Build result. $result[$i]['question'] = $question; $result[$i]['answers'] = $answers[0]; ++$i; } print_r($result); ?> </pre> Quote Link to comment Share on other sites More sharing options...
haku87 Posted April 14, 2007 Author Share Posted April 14, 2007 Hi, when I tested out with $data = <<<A wdafjklaklsdsklfa A; It is working. However, when i attempted to read it from a file and extract information out. It is not working anymore. Questions are still able to extract out but not those answer This is what I had in my text 1. Is php powerful? a.Yes b.No 2. Is GD library useful? a.Yes b.No /**************** End **********************/ $File = "./../quizbank/".$info['quizname'].".txt"; $Handle = fopen($File, 'r'); $content = file_get_contents($File); I extract it using the method file_get_contents($File). After that I called the function $data = ProcessQns($content); Why? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.