I have a plain text file (.txt) that consist of trivia questions in a format like so (small sample):
1. Who was the first president of the United States of America?
ANSWER: George Washington
2. What is the opposite of up?
ANSWER: Down
3. What country's capital is Paris?
ANSWER: France
The question # and question are on one line, and the answer is on the next line with a "ANSWER:" tag before the actual answer.
I want to take this data and convert it into CSV with the columns being Question #, Question, and Answer like so:
"1","Who was the first president of the United States of America?","George Washington"
"2","What is the opposite of up?","Down"
"3","What country's capital is Paris?","France"
Note: the period after the question # and the "ANSWER:" tag before the answer is removed.
How would I go about doing this in a PHP script that takes the plain text data and outputs CSV as shown above? I'm completely new to parsing (I think that's what it is called?) and any help is greatly appreciated.
In fact my code is really simple, but it will work :)
<?php
$text = '1. Who was the first president of the United States of America?
ANSWER: George Washington
2. What is the opposite of up?
ANSWER: Down
3. What country\'s capital is Paris?
ANSWER: France';
echo text_to_csv( $text );
function text_to_csv( $text = null ) {
$lines = explode( "
", $text );
$data = array();
$temp = array();
foreach( $lines as $line ) {
$line = trim( $line );
if ( empty( $line ) ) {
continue;
}
if ( preg_match( '/^([0-9]+)\.(.+?)$/', $line, $quest ) ) {
$temp[] = trim( $quest[1] );
$temp[] = trim( $quest[2] );
continue;
}
if ( preg_match( '/^answer\:(.+?)$/i', $line, $quest ) ) {
$temp[] = trim( $quest[1] );
$data[] = '"'.implode( '","', $temp ).'"';
$temp = array();
}
}
return implode( "
", $data );
}
?>