A basic PHP-based text-file parser with the ability to read/write content.
To use this file parser, include the utility like so include ('FileUtility.php');
in the header of your file.
Next initialize the class by passing the name of a text file:
$file = new FileUtility('content.txt');
In order for this class to be able to parse the content, the content must be formatted like so:
[header]
project: Programming Test
budget : 4.5
accessed :205
[meta data]
description : This is a tediously long description of
the programming test that you are taking. Tedious
isn't the right word, but it's the first word that
comes to mind.
correction text: I meant 'moderately' not 'tediously'.
[ trailer ]
budget:all out of budget
The section name is surrounded by [
and ]
brackets.
Then each section line is then defined by a key, which comes before the :
and the content that comes after the :
.
Once the class has been constructed using a valid file name, you will have access to the following methods:
This sets a value with the associated key in a valid section. Once complete, this updates the file that was used within the constructor.
$file->setSectionValue('meta data', 'description', 'This is a very basic description that totally makes sense.');
- sectionName - string - The name of the section that you want to set
- key - string - The key of the content that you want to set
- content - string - The content that you would like to associated with the key
Returns the updated section
Renames the header of the section
$file->setSectionHeader('trailer', 'footer');
- sectionName - string - The name of the section that you want to rename
- newSectionName - string - The new name of the section
Returns the updated section
Returns the content associated with the section name and content key
$content = $file->getValue('trailer', 'budget');
echo $content;
- sectionName - string - The name of the section associated with this content
- key - string - The key of the content that you want to retrieve
string
Same of that of the getValue
method, except this value is printed in the page.
$file->printValue('trailer', 'budget');
- sectionName - string - The name of the section associated with this content
- key - string - The key of the content that you want to retrieve
print_r
printed string