Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.
Reading/Parsing and Writing YAML files, PHP Symfony
In this tutorial, we will explore how to use PHP for writing and parsing YAML files. Whether you are a PHP developer who wants to work with YAML files or just someone curious about this format, this post will offer you the necessary information to get started.
What is a YAML file?
A YAML (short for “YAML Ain’t Markup Language”) file is a human-readable text file format used for data serialization. Software applications frequently utilize this format to store configuration settings or exchange data between different systems.
Designers made YAML to be more human-readable than JSON and XML, despite its similarities to both formats. It uses a syntax that is based on indentation and uses colons and dashes to separate key-value pairs and lists.
This is a snippet of what you might expect to see in a YAML file:
# Example yaml file name: Gav age: 37 email: [email protected]
This YAML file contains three key-value pairs, where the keys are “name”, “age”, and “email”, and the values are “Gav”, 37, and “[email protected]”.
The # character is used to indicate a comment, which is handily ignored by the parser.
Parsing and writing YAML files using Symfony Yaml component
Now that I have introduced you to YAML, let’s explore how to write and parse YAML files in PHP.
Installing Symfony Yaml using composer
If you want to follow along with this tutorial, it is a pre-requisite that you have Composer installed. To install the Symfony YAML component, open a terminal in the root of your project and enter the following command:
composer require symfony/yaml
Now that we have the Symfony reader installed we can include the namespace so that the YAML object is available to us. First we ‘require’ our composer ‘autoload.php’ file, and then we use the Symfony YAML namespace.
require "vendor/autoload.php"; use Symfony\Component\Yaml\Yaml;
The next part is simple; we can use the reader to parse the file.
The parseFile() method returns an associative array representing the parsed YAML data. We can then access the parsed data just like any other array.
$filePath = ''; $content = Yaml::parseFile($filePath); var_dump($content);
So how would this look when we actually parse a file? Let’s assume we parsed the previously exampled YAML file:
name: Gav age: 37 email: [email protected]
require "vendor/autoload.php"; use Symfony\Component\Yaml\Yaml; // Load the YAML file $content = Yaml::parseFile('/path/to/file.yaml'); // Access the parsed data echo $content['name']; echo $content['age']; echo $content['email'];
In these snippets we’re echoing out the name, age, and email by accessing the $content array. All you need to do is replace these keys with the appropriate keys from your own YAML file.
Writing YAML files in PHP
OK, so we can parse! Now we can explore how to write.
require "vendor/autoload.php"; use Symfony\Component\Yaml\Yaml; // Define the data to be written to the YAML file $input = [ 'user' => [ 'name' => 'Gav', 'website' => 'https://www.gavsblog.com' ] ]; // Convert the data to YAML format $yaml = Yaml::dump($data); // Write the YAML data to a file file_put_contents('/path/to/file.yaml', $yaml);
First we define the array of data that we want to write to a YAML file, then use the Yaml::dump() method to convert the data into YAML format.
Finally, we use file_put_contents() to write the YAML formatted data to a file located at /path/to/file.yaml.
Note: If the file does not exist, file_put_contents() will be create it for us. If the file already exists, the new YAML data will overwrite its contents unless we pass the FILE_APPEND boolean parameter as true (the third parameter).
You might like:
Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.
In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.
Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.
Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.
Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.