简体   繁体   中英

Modify yaml file with 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 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.....

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 = ' http://i.imgur.com/nOptw.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM