I have made a simple form with textfields, when i submit a button it wrties all textfield values into a .txt file. Here is an example of the .txt file content:
-----------------------------
How much is 1+1
3
4
5
1
-----------------------------
The 1st and last line ----
is there to just seperate data. The 1st line after the ----
is the question
, the before the bottom seperator (1) is the true answer
, and all the values between question
and true answer
are false answers
.
What i want to do now is echo out the question
, false answers
and true answer
, seperatly:
echo $quesiton;
print_r ($false_answers); //because it will be an array
echo $true answer;
I think the solution is strpos
, but i dont know how to use it the way i want it to. Can i do somethinglike this? :
Select 1st line (question) after the 1st seperator
Select 1st line (true answer) before the 2nd seperator
Select all values inbetween question and true answer
Note that im only showing one example, the .txt file has a lot of these questions seperated with -------.
Are my thoughs correct about using strpos to solve this? Any suggestions?
Edit: Found some function:
$lines = file_get_contents('quiz.txt');
$start = "-----------------------------";
$end = "-----------------------------";
$pattern = sprintf('/%s(.+?)%s/ims',preg_quote($start, '/'), preg_quote($end, '/'));
if (preg_match($pattern, $lines, $matches)) {
list(, $match) = $matches;
echo $match;
}
I think this might work, not sure yet.