I have a text file Qfile.txt and the contents are follows, and want to create another file with the same informations and but answers are diffrent. Qfile1.txt,Qfile2.txt
Qfile.txt
Question "What is your age?"
Answer ""
Question "What you doing?"
Answer ""
Question "What is you name?"
Answer ""
Qfile1.txt
Question "What is your age?"
Answer "25"
Question "What you doing?"
Answer "chatting"
Question "What is you name?"
Answer "Midhun"
I want to read the questions from the Qfile.txt and store the informations to Qfile1.txt with in PHP. I wrote some code but the pattern matching is not working:
$contents=file_get_contents("Qfile.txt");
foreach(/*bla bla*/)
{
$pattern = "/Question \"".preg_quote($id, '/')."\"\nAnswer \"\"/";
$string = str_replace('"', '\"', $string);
$replacement = "Question \"$id\"\nAnswer \"". $string . "\"";
$result = preg_replace($pattern, $replacement, $contents);
}
The preg_replace($pattern, $replacement, $contents);
is not working.