简体   繁体   English

使用PHP脚本修改yaml文件

[英]Modify yaml file with PHP script

I have a common.yml file with the following data : 我有一个带有以下数据的common.yml文件:

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. 我正在尝试创建一个php脚本,它将'src'属性编辑为新图像,我从我下载的新json文件中获取。 The problem is how do i edit the src of these 2 images. 问题是如何编辑这两个图像的src。 I tried to use Symfony Yaml component Dumper but dont know how to use it to update a particular part of my file. 我试图使用Symfony Yaml组件Dumper,但不知道如何使用它来更新我的文件的特定部分。

Please help..... 请帮忙.....

As you said you have to use the Symfony Yaml component. 正如您所说,您必须使用Symfony Yaml组件。

For example you can access the "src" data : 例如,您可以访问“src”数据:

$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 '. 在这里,您的数据可以访问=' 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 希望这可以帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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