[英]Change PHP script to printing file for Each 2000 line out from a array
我需要一些幫助來使此功能正常運行。
我正在嘗試將easylist.txt轉換為特定的Json格式並導入到我的Untangle防火牆。
波紋管是我正在運行以將其轉換的PHP腳本。 問題是導入期間限制為大約2000行。 因此,我需要它為每2000行創建一個Export文件。 並根據需要執行盡可能多的文件。
在此問題上的任何幫助都將由您承擔。
感謝WebFooL
<?php
$Content = "./easylist.txt";
$lines = file($Content);
$vJsonFileName = "AD-Blocker". ".json";
$badcharacters = array("#",'"', "'", "[", "]", "\n", "\t");
header("Content-type: application/json");
header("Content-Disposition: attachment; filename=$vJsonFileName");
header('Cache-Control: public');
echo "[";
foreach($lines as $str){
$cleanstr = str_replace($badcharacters, "", $str);
echo '{"enabled":true,"string":"'.$cleanstr.'","javaClass":"com.untangle.uvm.node.GenericRule"},';
}
echo "]";
?>
假設您可以隨意分割輸入數據(即條目不跨越多行,並且每一行都是“原子的”),那么您只需提取所需的行數即可
$Content = "./easylist.txt";
$lines = file($Content);
$linesSplit = array_chunk( $lines, 2000 );
順便說一句,您真的需要刪除“壞字符”嗎? 您是否使用json_encode進行了調查?
您無法下載多個文件,因此以下內容將創建多個文件。 這里有一篇關於壓縮文件的文章: 如何[遞歸]在PHP中壓縮目錄?
<?php
$Content = "./easylist.txt";
$lines = file($Content);
$vJsonFileName = "AD-Blocker.zip";
$badcharacters = array("#",'"', "'", "[", "]", "\n", "\t");
$f = 1;
foreach($lines as $str)
{
$c++;
if($c==2000)
{
$f++;
}
$cleanstr = str_replace($badcharacters, "", $str);
$json[$f] = '{"enabled":true,"string":"'.$cleanstr.'","javaClass":"com.untangle.uvm.node.GenericRule"},';
}
foreach($json as $f => $arr)
{
file_put_contents('file'.$f,'['.implode('',$arr).']');
}
//zip up all the files here to $vJsonFileName
header("Content-type: application/json");
header("Content-Disposition: attachment; filename=$vJsonFileName");
header('Cache-Control: public');
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.