<?php
ini_set('error_reporting', E_ALL);
$Question = array();
$DoneQuestion = array();
echo "Starte Quizkonverter.\n";
function startsWith($haystack, $needle)
{
return $needle === "" || strpos($haystack, $needle) === 0;
}
function endsWith($haystack, $needle)
{
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
function isCompleteQuestion()
{
if($Question['Question'] != null && $Question['Answer'] != null && $Question['Question'] != "" && $Question['Answer'] != "") return true;
return false;
}
function QuestionExists($Question)
{
$i = 0;
while($i < count($DoneQuestion))
{
if($DoneQuestion[$i] == $Question) return true;
}
return false;
}
echo "Öffne Quizdatei und Destination Datei:\n";
$Quizfile=fopen("quiz.txt", "r");
$Destfile=fopen("quiz.sql", "c");
if($Quizfile && $Destfile)
{
$SQLSTART = "INSERT INTO `Quiz` (`Question`, `Answer`, `Regexp`, `Category`, `Level`) VALUES ";
fwrite($Destfile, );
while(($buffer = fgets($Quizfile, 4096)))
{
$line = trim($buffer);
if(startsWith($line, "#")) continue;
elseif(empty($line))
{
if(isCompleteQuestion() && !QuestionExists($Question['Question']))
{
$String = ",\n('".$Question['Question']."', '".$Question['Answer']."', '".$Question['Regexp']."', '".$Question['Category']."', '".$Question['Level']."')"
fwrite($Destfile, $String);
}
$Question['Question'] = "";
$Question['Answer'] = "";
$Question['Regexp'] = "";
$Question['Category'] = "";
$Question['Level'] = "";
}
elseif(startsWith($line, "Category:")) $Question['Category'] = trim(str_replace("Category:", "", $line));
elseif(startsWith($line, "Question:")) $Question['Question'] = trim(str_replace("Question:", "", $line));
elseif(startsWith($line, "Answer:")) $Question['Answer'] = trim(str_replace("Answer:", "", $line));
elseif(startsWith($line, "Regexp:")) $Question['Regexp'] = trim(str_replace("Regexp:", "", $line));
elseif(startsWith($line, "Level:")) $Question['Level'] = trim(str_replace("Level:", "", $line));
}
}
fclose($Quizfile);
fclose($Destfile);
echo "Fertig.";
?>
Comments
0 B
|👍
/👎