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);
}
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));
}
- 2 回答
- 0 关注
- 125 浏览
添加回答
举报