Skip to content

Instantly share code, notes, and snippets.

@MostafaRabia
Last active July 22, 2020 02:15
Show Gist options
  • Save MostafaRabia/d86af5d02d422c7fb8b5b9276c3e5de0 to your computer and use it in GitHub Desktop.
Save MostafaRabia/d86af5d02d422c7fb8b5b9276c3e5de0 to your computer and use it in GitHub Desktop.
<?php
/*
* Algorithm for put exams by copy & paste in easy way.
* Texts like in $string will be accepted, else won't.
* Must every question have number, unless won't counting here.
* Must enter key enterd to new line between questions and its answers, unless it won't work.
* The correct answer defines by *, and degree of its question by counting *.
* Nothing else is required, you are free to set what will be there after number of question, Like 1- or 1., you can add more in ([-.\/:_]+).
*/
// In production use it
//$string = htmlspecialchars(strip_tags($_POST['textarea']));
// In production remove it, just for test.
$string = "
1......----//// What's your name?
A.
B.*
C.
2- What's your name?
A.
B.
C.*
3_ What's your name?
A.
B.*
4/ What's your name?
A.*
B.
";
// Get questions based on numbers of questions, must questions have number unless it won't work.
// If user type in answer 4.*, it's "without ([ ]+[\p{L}|\d]|[\p{L}|\d])" will be question, so ([ ]+[\p{L}|\d]|[\p{L}|\d]) is important to check if it's number and dot and (space or space then any character), but it's get first word of it and delete it from all question, so i use "$split_questions[$i-1]" to get this word.
$split_questions = preg_split('/([0-9]+)([-.\/:_]+)([ ]+[\p{L}|\d]|[\p{L}|\d])/u',$string,-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
// There is empty in index 0, i don't know why...
unset($split_questions[0]);
// For count numbers of question, won't change in "continue" like "$i" in loops.
$count_questions_number = 1;
// Both for test, just to look the finally result, in production delete those.
$question = [];
$answers = [];
// Let's start magic!
for($i=0;$i<=count($split_questions);$i++){
// Check if there is anything empty, just run don't look on it.
if (empty($split_questions[$i])){
continue;
}
// Check if it's question, or not, I based on there is no question lower than 3 words and the question must have 1 letter.
if (!preg_match('/[\p{L}]/u',$split_questions[$i])||strlen(str_replace(' ','',$split_questions[$i]))<=3){
continue;
}
// Split question from its answers, based on enter key enterd, or based on new lines between them.
$split_question_and_answers = preg_split('/\n|\r\n?/',$split_questions[$i]);
// We got question in $split_question_and_answers[0], and its number in $count_questions_number, you are free to use it.
// Remove [] in production, use your queries in this loop.
// trim() to remove spaces in first and last of string.
// $split_questions[$i-2] for get what user typed after number, you free to change it.
$question[] = $count_questions_number.''.$split_questions[$i-2].' '.$split_questions[$i-1].''.trim($split_question_and_answers[0]);
// Loop for answers, starts 1 because 0 it's question.
for($n=1;$n<count($split_question_and_answers);$n++){
if (empty($split_question_and_answers[$n])){
continue;
}
$check_if_correct = explode('*',$split_question_and_answers[$n]);
// If he write "ans*wer" or "answer*." and get answer by "$check_if_correct[0]" it will "ans" or "answer" with out dot, this for fix.
$get_answer = trim(str_replace('*','',$split_question_and_answers[$n]));
if (count($check_if_correct)>1){
// It's correct.
// Remove every [] in production, use your queries here.
$answers[$count_questions_number.''.$split_questions[$i-2].' '.$split_questions[$i-1].''.trim($split_question_and_answers[0])][] = $get_answer;
// To get its degree, you free to use it.
$get_degree = str_replace('*','*',$split_question_and_answers[$n],$get_degree_of_this_question);
// Use $get_degree_of_this_question to get, it's looping how many replaced * with *, If it's stupid way use your own, and till me :)
}else{
// You have the answer in $get_answer.
// Remove every [] in production, use your queries here.
$answers[$count_questions_number.''.$split_questions[$i-2].' '.$split_questions[$i-1].''.trim($split_question_and_answers[0])][] = $get_answer;
}
}
$count_questions_number++;
}
// Remove in production.
print_r($question);
echo "<br><br>";
print_r($answers);
/*
* Thanks, And i'm sorry if there is anything wrong or stupid, I still learning, And sorry for my bad english too.
* If you need to import word and read it, you are free, And excel too but it's way not like this, just
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment