Jump to content

Extracting Data From BlackBoard Format.


haku87

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/45783-extracting-data-from-blackboard-format/
Share on other sites

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]

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

 

 

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

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

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?

 

 

^ ### 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]+)

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.

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

  • 2 weeks later...

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.