Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Yaml with PHP [closed]

Tags:

html

php

yaml

Someone knows how I could read a yaml file with php? What I want is something like a reader.

Me and my friends are creating a Minecraft Server and we want connect the server to site. For that we need a reader to read the YAML file that is in our server files. This file calls users.yml and inside it is the next code:

users:
      80679a11-1d47-3a0e-8346-4790ee4304fc:     <<<< Player Code.
        group:
        - Admin                                 <<<< Player Group.
        options:
          name: JamesMarch                      <<<< Player Nick Name.
      56874a35-8f52-5f2c-7843-7788je9670tb:     <<<< Player Code.
        group:
        - Admin                                 <<<< Player Group.
        options:
          name: Angelow98                       <<<< Player Nick Name.
      55026444-cb34-3a27-a270-d7d07fccca0a:     <<<< Player Code.
        group:
        - Helper                                <<<< Player Group.
        options:
          name: iDatSnoow_                      <<<< Player Nick Name.

When a new player connects to the server to play, this file automatically creates a new paragraph like this (This is a new player):

  84569a84-5d77-3a5e-8547-4720ee4304fc:   <<<< Player Code.
    group:
    - NewPlayer                           <<<< Player Group (NewPlayer is the default group)
    options:
      name: mumiant_                      <<<< Player Nick Name.

To the player becomes a Administrator, for example, one of the administrators, has enter this command: /manuadd (player name) Admin and it will automatically edit in users.yml

In our website, on home page, we would like to show, in a box, the most important groups (helpers and administrators) like this simple HTML code:

<h1>Staff</h1>
    <h2>Administrators</h2>
        <p>Angelow98</p>
        <p>JamesMarch</p>
    <h2>Helper</h2>
        <p>iDatSnoow_</p>

In conclusion, what we want is that it work automatically. This is, when someone becomes Administrator, the PHP Reader will read the YAML code and it will print on website, like the HTML code above.

like image 423
João Ramos Avatar asked Apr 03 '16 07:04

João Ramos


People also ask

Is YAML machine readable?

YAML (/ˈjæməl/ and YAH-ml) (see § History and name) is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted.

How do I view a YAML file?

We can read the YAML file using the PyYAML module's yaml. load() function. This function parse and converts a YAML object to a Python dictionary ( dict object). This process is known as Deserializing YAML into a Python.

What is PHP YAML?

The PHP YAML extension provides PHP functions for parsing and serializing files and text containing YAML markup.

How do I open a YAML file in my browser?

How to open a YAML file. You can open a YAML file in any text editor, such as Microsoft Notepad (Windows) or Apple TextEdit (Mac). However, if you intend to edit a YAML file, you should open it using a source code editor, such as NotePad++ (Windows) or GitHub Atom (cross-platform).


1 Answers

You can parse YAML and dump an array to YAML using symfony/yaml:

use Symfony\Component\Yaml\Yaml;
$yaml = Yaml::parse(file_get_contents('/path/to/file.yml'));
$yamlString = Yaml::dump($yaml);

Now to parse your example, I replaced the <<<< with valid YAML comments:

$data = \Symfony\Component\Yaml\Yaml::parse('users:
  80679a11-1d47-3a0e-8346-4790ee4304fc:     # Player Code.
    group:
    - Admin                                 # Player Group.
    options:
      name: JamesMarch                      # Player Nick Name.
  56874a35-8f52-5f2c-7843-7788je9670tb:     # Player Code.
    group:
    - Admin                                 # Player Group.
    options:
      name: Angelow98                       # Player Nick Name.
  55026444-cb34-3a27-a270-d7d07fccca0a:     # Player Code.
    group:
    - Helper                                # Player Group.
    options:
      name: iDatSnoow_                      # Player Nick Name.');

Now let's group all players by their first assigned group:

$groups = array();
foreach ($data['users'] as $playerCode => $player) {
    $firstGroupName = $player['group'][0];
    $groups[$firstGroupName][$playerCode] = $player;
}

$groups now looks like this:

Array
(
    [Admin] => Array
        (
            [80679a11-1d47-3a0e-8346-4790ee4304fc] => Array
                (
                    [group] => Array
                        (
                            [0] => Admin
                        )
                    [options] => Array
                        (
                            [name] => JamesMarch
                        )
                )
            [56874a35-8f52-5f2c-7843-7788je9670tb] => Array
                (
                    [group] => Array
                        (
                            [0] => Admin
                        )
                    [options] => Array
                        (
                            [name] => Angelow98
                        )
                )
        )

    [Helper] => Array
        (
            [55026444-cb34-3a27-a270-d7d07fccca0a] => Array
                (
                    [group] => Array
                        (
                            [0] => Helper
                        )

                    [options] => Array
                        (
                            [name] => iDatSnoow_
                        )
                )
        )
)

If you pass that array to a PHP template, you could achieve your output like this:

<?php foreach($groups as $group => $players): ?>
<h1><?= $group ?></h1>
<?php foreach ($players as $playerCode => $player): ?>
<p><?= $player['options']['name'] ?> </p>
<?php endforeach; ?>
<?php endforeach; ?>
like image 64
Marcel Voigt Avatar answered Sep 21 '22 01:09

Marcel Voigt