簡體   English   中英

從特定行在文件(YML)中插入行

[英]Insert lines in a file (YML) from specific line

我想使用PHP從文件中的特定行向文件中插入行。

我的文件acme.yml具有以下行shortcuts: 我想在前面插入一些行。

#newlines here
shortcuts:

我在PHP手冊中看到了函數file_get_contentsfile_put_contents ,但是我不知道這是否是最好的方法。

<?php
    $file = 'acme.yml';
    $newlines = array($line1, $line2, $line3);

    $current = file_get_contents($file);

    #TODO:

    file_put_contents($file, $current);
?>

也許在yml文件中有更好的方法。

<?php

$file = 'acme.yml';
$newlines = array($line1, $line2, $line3);

$current = file_get_contents($file);

$current = str_replace("shortcuts:\n", implode("\n", $newlines) . "\nshortcuts:\n", $current);

file_put_contents($file, $current);

?>

嘗試這個。

暫無
暫無

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

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