Introduction to YAML and Parsing in PHP
This article serves as a brief introduction to YAML and its usage in PHP. YAML stands for YAML Ain’t Markup Language which is a recursive acronym. YAML is a data serialization language used to store and transmit data. It’s a human-friendly markup language describing data in a more readable fashion. It’s similar to other data serialization representations like JSON and XML both of which we use to store and transmit data between different applications. For instance, API uses JSON and XML for exchanging data between two different platforms. YAML is not widely used for API. It’s especially used to store the configuration of applications, page settings and more. Symfony, a PHP framework, uses YAML to store its configurations. YAML files can use either .yaml
or .yml
extension. Both extensions work the same way. Now, the theory is enough. Let’s get into the pragmatic approach.
To parse YAML file in PHP, we use either of the following functions:
- yaml_parse_file — Parse a YAML stream from a file
- yaml_parse_url — Parse a YAML stream from a URL
- yaml_parse — Parse a YAML stream
Before we start with the examples, let’s realize that these functions are not bundled up with PHP by default. Thus, we will download and install YAML extension using PECL.
Installation & Configuration:
1 2 3 4 5 6 7 |
sudo apt-get install gcc make autoconf libc-dev pkg-config sudo apt-get install libyaml-dev php-pear sudo pecl channel-update pecl.php.net sudo pecl install yaml sudo bash -c "echo extension=yaml.so > /etc/php/7.2/mods-available/yaml.ini" sudo phpenmod yaml sudo service apache2 restart |
Note: The command sudo bash -c "echo extension=yaml.so > /etc/php/7.2/mods-available/yaml.ini"
writes to yaml.ini
for PHP version 7.2
. If you are using other version, for example, 7.0
or 7.1
, you need to replace 7.2
with the respective version.
To ensure the YAML extension is enabled, you can use either of the following methods:
METHOD 1:
1 |
php -m |
The command lists out all the modules enabled. Check for YAML from the list. If it exists, YAML support is enabled and you are good to go. Now you can use YAML parse functions.
METHOD 2:
1 2 3 4 5 |
<?php phpinfo(); ?> |
Save the above code to a PHP file with your choice of a filename and execute the script in the browser. Now you might see the output as in the following image.
Now, press ctrl+f
to pull out find
toolbar and type yaml
and keep searching until you see the screen as in the following image.
If any of these two methods shows that YAML extension is enabled, you are good to go. Otherwise, leave us your message in the comments section, we are ready to help you resolve the issue.
Before we start with an example, let’s have a brief look at the signature of YAML functions.
yaml_parse_file ( string $filename
[, int $pos
= 0 [, int &$ndocs
[, array $callbacks
= NULL
]]] )
yaml_parse_file
accepts $filename
parameter as a mandatory. Other parameters are optional. In most cases, we only use $filename
.
yaml_parse_url ( string $url
[, int $pos
= 0 [, int &$ndocs
[, array $callbacks
= NULL
]]] )
It’s similar to yaml_parse_url
except HTTP URL is passed in lieu of filename.
yaml_parse ( string $input
[, int $pos
= 0 [, int &$ndocs
[, array $callbacks
= NULL
]]] )
yaml_parse
is same as the other two, except the input is accepted as a string representation of YAML.
Now, let’s get started with a short example.
Let’s assume that the following YAML representation is stored in a file called users.yaml
.
users.yaml
1 2 3 4 5 6 7 |
users: - name: "John Doe" age: 23 email: johndoe@example.com - name: "Jane Doe" age: 20 email: janedoe@example.com |
yaml.php
1 2 3 4 5 6 |
<?php $parsed = yaml_parse_file("users.yaml"); echo "<pre>" . print_r($parsed, true) . "</pre>"; ?> |
The output of the above code appears to be as in the following screenshot.
As simple as that. It’s similar to how you do with json_decode
to deserialize JSON data to PHP compliant array or object.
The article intended to be brevity of YAML usage in PHP. If you want an article explaining YAML in detail with few more examples in PHP, encounter any issues during installation and configuration, find any mistakes in the article, please let us know all that in the comments.
Also, if you have any other queries, request for an article of your choice of technology, drop us a message in the comments.
Have a nice day! Happy coding!
Great, thanks for sharing us.
Thanks for sharing this great information. This is a great list for those people who looking Corporate Themes to build their website.
Hi, if you don’t wanna or cannot install a PHP extension you can use https://github.com/dallgoot/yaml which is a YAML Parser/Loader written in PHP