我需要一些帮助才能让这件事按我不愿的方式进行。
我正在尝试将easylist.txt转换为特定的Json格式,并导入到我的Untangle防火墙中。
下面是我正在运行的PHP脚本,用来转换它。问题是在导入过程中有大约2000行的限制。因此,我需要它为每2000行创建一个导出文件。并根据需要创建任意数量的文件。
在这件事上的任何帮助都是假定的。
谢谢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 "]";
?>发布于 2012-03-24 16:52:53
假设您可以任意拆分输入数据(即条目不会跨越多行,并且每行都是“原子的”),那么您可以只提取所需的行数
$Content = "./easylist.txt";
$lines = file($Content);
$linesSplit = array_chunk( $lines, 2000 );顺便说一句,你真的需要删除“坏字符”吗?您是否曾使用json_encode进行过调查
发布于 2012-03-24 16:58:08
您不能下载多个文件,因此下面的操作将创建多个文件。这里有一篇关于压缩文件的帖子:How to [recursively] Zip a directory in 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');
?>https://stackoverflow.com/questions/9850313
复制相似问题