Answer the question
In order to leave comments, you need to log in
How to convert strings from a TXT file to JSON using PHP with conditions?
There is a .txt file with text inside.
$text_list = file('../templating/a0/update-01.txt');
Answer the question
In order to leave comments, you need to log in
You open a file with fopen, and read it line by line with fopen in a loop until you reach the end of the file. In a loop, you write logic that will process the string and add it to json.
php.net/manual/en/function.fread.php
First, decide on the format of your file, it's good that it be XML or something like that.
Compose the structure and possible control characters - the specification.
Then do as Minifest says.
Use regular expressions in the loop body. "PHP preg_match()"
For testing regular expressions, it is convenient to use a service like: https://regex101.com/
Then you can collect the data that you need to collect in an array or even an object.
After parsing is complete, use the "PHP json_encode()" function to convert the object or array to JSON
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question