【问题标题】:adding string after every 'n' number of lines in a file using php使用php在文件中的每'n'行后添加字符串
【发布时间】:2018-09-22 08:44:32
【问题描述】:

我有一个包含大约 1800 行数据的 .lst(playlist) 文件。每行都包含一个指向正在我的广播电台上播放的音频文件的 URL。 问题是我需要在该文件中的每 'n' 行之后添加一些广告的 URL。

有10个广告URL,每5行后需要添加1个URL。

示例:广告的 URL 格式如下:file1.mp3, file2.mp3 ... file10.mp3

它们将像这样添加:file1.mp3 在第 5 行,file2.mp3 在第 10 行,file3.mp3 在第 15 行,依此类推。插入file10.mp3 后,会再次从file1.mp3 开始。希望你能理解。

到目前为止,我已经设法编写了以下代码,但它只需要添加一个字符串,并且必须手动告诉要添加字符串的行号。无法弄清楚执行上述工作的循环逻辑。

$url = "/home/station/content/file1.mp3";
$line_number = 5; //add above URL on line number 5
$contents = file('playlist.lst', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if($line_number > sizeof($contents)) {
    $line_number = sizeof($contents) + 1;
}
array_splice($contents, $line_number-1, 0, array($url));
$contents = implode("\n", $contents);
file_put_contents('playlist.lst', $contents);

我怎样才能做到这一点?

【问题讨论】:

  • 具体的问题是什么?您可以从 5 开始循环并以 5 为步长增加,直到达到广告数量的 5 倍。
  • 不要忘记第 15 行的项目在第一次插入后将在第 16 行,在第二次插入后在第 17 行...
  • @Justinas 哇,我完全忘记了这个……好建议!

标签: php string file


【解决方案1】:

您应该以这种方式进行循环。

    $line_number = 5;
    $line_count = 0;

    for($i=0; $i < sizeof($contents); $i++)
    {
        $line_count = $line_count +1; // start with Line 1 that +1

        if($line_count == $line_number)
         {
            // do your magic code of adding a line

           $line_count = 0; // refresh counting from the beginning
          }

    }

【讨论】:

    【解决方案2】:

    你可以这样做,用一个简单的循环:

    //changing it to a "model" string, we are going to add the correct file number later
    $url = "/home/station/content/file";
    $contents = file('playlist.lst', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $count = 0;
    $AddCount = 1;
    //Loop until there is nothing left in our radio list
    while ($count < sizeof($contents)) {
        //if we have a multiple of 5, we are inserting an ad
        if (($count % 5) == 0) {
            // to know wich ad to add we use our AddCounter
            $tmp = $url . strval($AddCount) . ".mp3";
            //As suggested by Justinas, don't forget that each time you had a line you need to also increase the index for the next one using $count%5 to know how many lines you already added
            array_splice($contents, $count - 1 + ($count % 5) , 0, array($tmp));
            $AddCount += 1;
            if ($AddCount > 10)
                $AddCount = 1;
        }
        $count += 1;
    }
    $contents = implode("\n", $contents);
    file_put_contents('playlist.lst', $contents);
    

    这样,您甚至不必自己处理广告文件的选择,只要它们都像您说的那样格式化。

    【讨论】:

    • 谢谢,它按预期工作,但只到第 915 行。它添加广告 URL 直到第 915 行,然后停止。
    • 是不是报错了?或者程序的其余部分是否按预期工作?我可能会看到问题出在哪里,将根据您的回答进行更新!
    • 我猜你确定你也有超过 915/920 行?
    • @Vikalp 我非常迟钝,这是修复(在我的帖子中也更新了),我使用的是$size,它包含你的数组的大小......在我们添加大量之前里面的东西,我只是替换了循环条件,所以我总是循环 sizeof($contents) 而不是 $size 以确保我正在检查数组的最后一个已知大小
    【解决方案3】:

    您可以使用array_chunk 将您的阵列拆分为$line_number。然后,使用array_map() 将您的广告添加到每个组。最后,您可以简化为线性阵列。您可以使用sprintf() 格式化$url

    $url = "/home/station/content/file%d.mp3"; // use %d to use using sprintf()
    $line_number = 5; //add above URL on line number 5
    $counter = 1;
    
    $contents = file('playlist.lst', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
    // split in group of $line_number
    $groups = array_chunk($contents, $line_number);
    // for each group:
    $groups = array_map(function($arr) use ($url, &$counter) { // pass $counter as reference
        // format the link 
        $adv = sprintf($url, $counter++) ;
        // restart to 1 if greater than 10
        if ($counter > 10) $counter = 1;
        // append to group
        $arr[] = $adv;
        return $arr ;
    },$groups);
    // transform to linear array
    $contents = array_reduce($groups, 'array_merge', array());
    // save new file
    file_put_contents('playlist.lst', implode("\n", $contents));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 2017-04-10
      • 2011-02-21
      • 1970-01-01
      • 2017-03-21
      相关资源
      最近更新 更多