Modify yaml file with PHP script

php, symfony, yaml

Solution

As you said you have to use the Symfony Yaml component.

For example you can access the "src" data :

$yaml = Yaml::parse(file_get_contents($this->container->get('kernel')->getRootDir() .'/config/common.yml'));

$srcData = $yaml['main']['schred']['viral']['image1']['src'];

Here, your data is accessible = 'https://i.stack.imgur.com/ElkSn.jpg'. Next you can change value and update your file :

$yaml['main']['schred']['viral']['image1']['src'] = $yourNewValue;

$new_yaml = Yaml::dump($yaml, 5);

file_put_contents($this->container->get('kernel')->getRootDir() .'/config/common.yml', $new_yaml);

Hope this can help you

Problem

I have a common.yml file with the following data : ``` main: shred: viral: image1: alt: Sunset src: 'http://i.imgur.com/nOptw.jpg' image2: alt: Fernie src: 'http://i.imgur.com/yfJaUoX.gif' ``` I am trying to make a php script which edits the 'src' attribute to new image i get from a new json file which i download. The problem is how do i edit the src of these 2 images. I tried to use Symfony Yaml component Dumper but dont know how to use it to update a particular part of my file. Please help.....

Original source