php - Why does my preg_replace() on multi-line file contents on Windows fail? -
I have a text file, Qfile.txt and the content is like this, and want to create another file with the same information Qfile1.txt, Qfile2.txt
Qfile.txt
questions are "yours" What is age?" Answer "Question" What are you doing? "Answer" question "What is your name?" Answer ""
Qfile1.txt
Question "What is your age?" Answer "25" question "What are you doing?" Answer "chat" question "What is your name?" Answer "Meehun"
I want to read the questions from Qfile.txt and store the information in PHP with Qfile1.txt. I wrote some code but pattern matching is not working:
$ contents = file_get_contents ("Qfile.txt"); Forex Currency (/ * bla bla * /) {$ pattern = "/ Question" "." Preg_quote ($ id, '/') "\" Ancer \ "\" / "; $ String = str_replace ('' ',' \" ', $ string); $ Change = "question \" $ id \ "\ n answer" \ "". $ String "\" "$ Result = preg_replace ($ pattern, $ replacement, $ content);}
unable to replicate; your code is optimally optimized, in fact Works fine for me I can think that the only thing is that you are writing it on a Windows machine and your text file contains the CRLF line terminator. You need to change your code: change preg_replace ($ patterns, $ replacement, $ content);
$ content = file_get_contents ("Qfile.txt"); Foreach (/ * bla bla * /) {$ pattern = "/ question" ". "Preg_quote ($ id, '/')." \ "\" \ "/"; $ String = str_replace ('' ',' \ "', $ string); $ change =" question \ "$ id \" $ 1 response \ "". $ String. "$ Result = preg_replace ($ pattern, $ substitution, $ content);}
$ pattern =
and $ Replacement =
are on lines I wrote this so that whatever line end pattern it will maintain (both supports, LF and CRLF) will be preserved.
Comments
Post a Comment