亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 讀取并連接多個遠程文件

PHP 讀取并連接多個遠程文件

PHP
達令說 2023-12-15 15:15:16
我使用此代碼讀取多個遠程文件:$filters = [ "https://example.com/file.txt", "https://example.com/file1.txt", "https://example.com/file3.txt" ]function parseFilterLists( $filters )  {    foreach( $filters as $filter ){      $file =  file_get_contents( $filter );      $parsed = preg_replace( '/!.*/', '', $file );      $parsed = preg_replace( '/\|\|([\w\d]+(?:\.[\w]+)+)(?:[\^\$=~].*)/', '*://*.$1/*', $parsed );     }    $output = array_filter( explode( "\n", $parsed ), function($url){      return preg_match('/^\*:\/\/\*\.[\w\d-]+\.[\w]+\/\*$/', $url);    });    return array_values(array_unique($output));  }我注意到輸出內容被截斷,就像只處理一個文件一樣,但我需要的是連接三個文件來操作它們。我怎樣才能實現這個目標?
查看完整描述

2 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

$parsed 的范圍僅限于 foreach 循環,因此 $output 僅包含循環最后一次迭代的結果。您應該在循環之前定義 $output,并將每次迭代的結果附加到其中。


<?php

$filters = ["https://example.com/file.txt", "https://example.com/file1.txt", "https://example.com/file3.txt"];


function parseFilterLists($filters)

{

    // Define a buffer for the output

    $output = [];

    foreach ($filters as $filter)

    {

        $file   = file_get_contents($filter);

        $parsed = preg_replace('/!.*/', '', $file);

        $parsed = preg_replace('/\|\|([\w\d]+(?:\.[\w]+)+)(?:[\^\$=~].*)/', '*://*.$1/*', $parsed);


        // Get the output for the file we're working on in this iteration

        $currOutput = array_filter(explode("\n", $parsed), function ($url) {

            return preg_match('/^\*:\/\/\*\.[\w\d-]+\.[\w]+\/\*$/', $url);

        });


        // Append the output from the current file to the output buffer

        $output = array_merge($output, $currOutput);

    }


    // Return the unique results

    return array_unique($output);

}


查看完整回答
反對 回復 2023-12-15
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

我想這會如你所愿


$filters = [ 

  "https://example.com/file.txt", "https://example.com/file1.txt", "https://example.com/file3.txt"

];


function parseFilterLists( $filters )

  {

    $files = [];

    foreach( $filters as $filter ){

      $files[] = file_get_contents( $filter );

    }

    $parsed = preg_replace( '/!.*/', '', implode("\n", $files));

    $parsed = preg_replace( '/\|\|([\w\d]+(?:\.[\w]+)+)(?:[\^\$=~].*)/', '*://*.$1/*', $parsed); 

    $output = array_filter( explode( "\n", $parsed ), function($url){

      return preg_match('/^\*:\/\/\*\.[\w\d-]+\.[\w]+\/\*$/', $url);

    });

    return array_values(array_unique($output));

  }


查看完整回答
反對 回復 2023-12-15
  • 2 回答
  • 0 關注
  • 190 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號