php - Text File Parsing and convert to JSON?
one text
Solution:
Here's a start at what you need. There are ways to make it more compact, but I'm going for clarity. You should add error-checking on the result of the preg_match call, so you know if it fails for some reason.
<?php
$text = file_get_contents('sample_data');
// The sample data has two sections,
// one for completed matches and one for upcoming matches.
// Extract the 'variable' text of these two sections:
preg_match('/^ *<\|> Most Recent Completed Matches: (.+) <\|> Upcoming Matches: (.+) <\|> *$/', $text, $reg_matches);
$completed_text = $reg_matches[1];
$upcoming_text = $reg_matches[2];
// Within the text of a section, the matches are separated by '|',
// so split the section-text on that character:
$completed_array = explode("|", $completed_text);
$upcoming_array = explode("|", $upcoming_text);
echo "\n";
echo "Most Recent Completed Matches\n";
echo "\n";
foreach ( $completed_array as $match_text )
{
echo "$match_text\n";
}
echo "\n";
echo "Upcoming Matches\n";
echo "\n";
foreach ( $upcoming_array as $match_text )
{
echo "$match_text\n";
}
?>
Source