如何将此 PHP 代码处理的数据保存到 CSV 或 TXT 文件
我得到了以下 php 代码,该代码当前处理一个 txt 文件,其中每行的数据如下所示(动物 > 宠物用品 > 鸟类用品),我正在重新格式化它以将其与我的数据库一起使用。
我想要做的是将输出数据保存到 CSV 或 TXT 文件,或者像当前一样在浏览器中打印。请注意,脚本中有 3 个打印部分。
任何帮助或建议将不胜感激。
set_time_limit(0);
//read and process txt.txt
$file_handle = fopen("txt.txt", "rb");
$j = 0;
while (!feof($file_handle)){
$line = fgets($file_handle);
$parts = explode('>', $line);
if(count($parts) == 1 ){
$parts[0] = trim($parts[0]);
$db[$j] = array("CatName"=>$parts[0], "CatID"=>$j+1, "PID"=>0, "Level"=>1);
print $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
} else {
$i =0;
$no = count($parts);
$t = $no-1;
while($t != -1){
$parts[$t] = trim( $parts[$t] );
$t = $t-1;
}
$k = $j-1;
while( $i != $j){
while($k > -1){
if( strcmp($parts[$i], $db[$k]["CatName"]) == 0 ){
$flag = 1;
}
$k = $k-1;
}
if($flag == 1){
if(strcmp($parts[$no-2], $db[$i]["CatName"]) == 0){
$found = $i;
}
}
$i = $i+1;
}
if($flag == 1){
$db[$j] = array("CatName"=>$parts[$no-1], "CatID"=>$j+1, "PID"=>$db[$found]["CatID"], "Level"=>$no);
print $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
}
if($flag == 0 && $no == 1){
$db[$j] = array("CatName"=>$parts[$no-1], "CatID"=>$j+1, "PID"=>0, "Level"=>$no);
print $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
}
}
$j = $j+1;
$flag = 0;
}
fclose($file_handle);
顺便说一句:我已经使用 stings 替换了 print 语句,然后将它们的输出保存到我的 csv 文件中,但它不起作用,它只返回空值。 使用的示例:
//print $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
$csv_data .= $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
I got the following php code that currently process a txt file with data that looks like this per line (Animals > Pet Supplies > Bird Supplies) and I'm reformatting this to use it with my database.
What I'd like to do is save the output data to a CSV or TXT file instead or printing it in my browser as it currently does. Notice that I have 3 print sections within the script.
Any help or advise will be much appreciated.
set_time_limit(0);
//read and process txt.txt
$file_handle = fopen("txt.txt", "rb");
$j = 0;
while (!feof($file_handle)){
$line = fgets($file_handle);
$parts = explode('>', $line);
if(count($parts) == 1 ){
$parts[0] = trim($parts[0]);
$db[$j] = array("CatName"=>$parts[0], "CatID"=>$j+1, "PID"=>0, "Level"=>1);
print $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
} else {
$i =0;
$no = count($parts);
$t = $no-1;
while($t != -1){
$parts[$t] = trim( $parts[$t] );
$t = $t-1;
}
$k = $j-1;
while( $i != $j){
while($k > -1){
if( strcmp($parts[$i], $db[$k]["CatName"]) == 0 ){
$flag = 1;
}
$k = $k-1;
}
if($flag == 1){
if(strcmp($parts[$no-2], $db[$i]["CatName"]) == 0){
$found = $i;
}
}
$i = $i+1;
}
if($flag == 1){
$db[$j] = array("CatName"=>$parts[$no-1], "CatID"=>$j+1, "PID"=>$db[$found]["CatID"], "Level"=>$no);
print $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
}
if($flag == 0 && $no == 1){
$db[$j] = array("CatName"=>$parts[$no-1], "CatID"=>$j+1, "PID"=>0, "Level"=>$no);
print $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
}
}
$j = $j+1;
$flag = 0;
}
fclose($file_handle);
BTW: I have already used stings to replaced the print statements and then save their outputs to my csv file, but it wont work, it only returns empty values.
Example used:
//print $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
$csv_data .= $db[$j]["CatName"]."\t".$db[$j]["CatID"]."\t".$db[$j]["PID"]."\t".$db[$j]["Level"]."\r\n";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想获取此脚本当前输出的内容并保存它,您可以使用 输出缓冲如Jared Farrish提到的,或替换您的
print
语句有陈述连接到字符串变量。例如,您可以声明一个空字符串,如下所示:
...,然后将每个
print
语句替换为 串联赋值,如.=
:If all you want to do is take what this script currently outputs and save it, you can either use output buffering as Jared Farrish mentioned, or replace your
print
statements with statements concatenating to a string variable.For example, you could declare an empty string like this:
... and then replace each
print
statement with a concatenation assignment like.=
: