Modify yaml file using php script

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 that edits the 'src' attribute for a new image that I get from the new json file that I upload. The problem is how to edit the src of these two images. I tried using the Dumper component of the Symfony Yaml component, but I don’t know how to use it to update a specific part of my file.

Please, help.....

+4
source share
1 answer

As you said, you need 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'];

= 'http://i.imgur.com/nOptw.jpg'. :

$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);

, .

+8

Source: https://habr.com/ru/post/1540613/


All Articles