簡體   English   中英

使用PHP腳本修改yaml文件

[英]Modify yaml file with PHP script

我有一個帶有以下數據的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'

我正在嘗試創建一個php腳本,它將'src'屬性編輯為新圖像,我從我下載的新json文件中獲取。 問題是如何編輯這兩個圖像的src。 我試圖使用Symfony Yaml組件Dumper,但不知道如何使用它來更新我的文件的特定部分。

請幫忙.....

正如您所說,您必須使用Symfony Yaml組件。

例如,您可以訪問“src”數據:

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

希望這可以幫到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM