Code: Select all
This is an **example** fill-in-the-blank **question**?Code: Select all
// I need the array to look like:
array ('example', 'question');
// and the question string to look like:
"This is an _ fill-in-the-blank _?"Moderator: General Moderators
Code: Select all
This is an **example** fill-in-the-blank **question**?Code: Select all
// I need the array to look like:
array ('example', 'question');
// and the question string to look like:
"This is an _ fill-in-the-blank _?"That's what I planned on doing. I just threw out the underscore as an example. What about finding the words in the first place?ole wrote:How about you output a paragraph of text with <input type="text">s wherever you have a blank to fill in instead.
Code: Select all
$string = "This is the **example** string that was taken from the **question** create form.";
preg_match('(**)', $string, $blanks);
echo "The words are: <br>";
foreach ($blanks as $word) {
echo $word."<br>";
}Into this (name and id tags left out for abbreviation).This is an {example} question. It could be {fairly long} with multiple {blanks}.
Here's what I've got so far, that isn't working.This is an <input type="text" /> question. It could be <input type="text" /> with multiple <input type="text" />.
Code: Select all
// $question = Array ( [id] => 7 [question] => An {example} question. [sequence] => 6 [type] => fb )
if ($question['type'] == 'fb') {
// convert string
$pos=0;
$open_positions=array();
while (strpos($question['question'], "{", $pos)) {
$open_positions[]=strpos($question['question'], "{", $pos);
$pos=strpos($question['question'], "{", $pos) + 1;
}
$pos=0;
$closed_positions=array();
while (strpos($question['question'], "}", $pos)) {
$closed_positions[]=strpos($question['question'], "}", $pos);
$pos=strpos($question['question'], "}", $pos) + 1;
}
if (count($open_positions) != count($closed_positions)) {
return FALSE;
}
$positions=array_merge($open_positions, $closed_positions);
sort($positions);
$counter=0;
$wordcount=1;
$new_question=$question['question'];
while (count($positions) > $counter) {
if ($counter == 0) {
$length = $positions[$counter + 1] - $positions[$counter] - 2;
} else {
line 132 $length = $positions[$counter + 1] - $positions[$counter] - 1;
}
$replace="<input type=\"text\" id=\"word_".$wordcount."\" name=\"word_".$wordcount."\" /></input>";
substr_replace($new_question, $replace ,$positions[$counter], $length);
$counter = $counter + 1;
$wordcount = $wordcount + 1;
}
$prepared_questions[]=array('id' => $question['id'], 'question' => $new_question,
'sequence' => $question['sequence'], 'type' => $question['type']);
}A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 2
Filename: student/take_test.php
Line Number: 132
Code: Select all
$str = preg_replace('~\b\{[^\}]+\}\b~', '<input type="text" />', $str);Code: Select all
<?php
$str = 'This is an {example} question. It could be {fairly long} with multiple {blanks}.';
function fixName($a)
{
return '<input type="text" name="' . preg_replace('#[^a-z0-9]#i', '_', $a[1]) . '" />';
}
$newstr = preg_replace_callback('#\{([a-z0-9 -]+)\}#i', 'fixName', $str);
echo $str, PHP_EOL, $newstr;
?>Code: Select all
This is an {example} question. It could be {fairly long} with multiple {blanks}.
This is an <input type="text" name="example" /> question. It could be <input type="text" name="fairly_long" /> with multiple <input type="text" name="blanks" />.