我正在尝试编写一个脚本,该脚本读取25个重文本文件的内容(每个约100行)。我希望我的脚本输出每个文本文件的第5行。我也在开发一个Windows工作站(在本地环境中使用Apache进行测试)。
每个文本文件都位于同一目录(即products / product1.txt)中,我想这会更容易。
第5行的每个文本文件都是这样的(带有不同的描述):
Product Desc: iphone
我还想知道在完成上述操作后,是否有可能让脚本删除文本'Product Desc:',以便只显示实际的产品描述。
请提供任何代码示例,因为我是noobie:)
答案 0 :(得分:1)
$dir = opendir('directory');
while($file = readdir($dir)){
if ($file != "." or $file != ".."){
$opened = file($file);
echo $opened[4]."<br />";
}
}
答案 1 :(得分:0)
PHP加载多个文件:
<?php
date_default_timezone_set("America/Edmonton");
$products=array();
$dir="../products/"; //Set this for whatever your folder
function getProduct($file) {
$fp=fopen($file,"r");
//Read five lines
for ($j=0;$j<5;$j++) $line=fgets($fp);
$GLOBALS["products"][]=substr($line,14);//Since products is a global var
fclose($fp);
}
$dp=opendir($dir);
while (($file = readdir($dp)) !== false) {
if ($file!="." && $file!="..")
getProduct($dir.$file);
}
closedir($dp);
//Review the product list
print_r($products);
?>
示例文件(one.txt):
line 1
line 2
line 3
line 4
Product Desc: iPhone
line 6
<强>快捷键:强>
因为我们只想要第五行,所以我们使用$j
循环读取前五行,每行覆盖最后一行。如果文件小于五行,您将从null
中获得$line
...您应该测试它(而不是将其添加到产品列表中)。最后,因为我们现在是字符串“Product Desc:
”的长度(你写的方式)我们可以把线的第一部分扔掉。这不是很糟糕,最好使用RegEx或字符串解析来确保正确的单词存在,然后使用冒号后面的数据。仍然......它回答了你原来的问题;)
你说可能会有更多的线路,这种方法只会将前5行加载到内存中(每次都写入最后一行)并在达到第5行后停止...这意味着比阅读时更严重的性能和内存优势将整个文件放入数组(file
),然后仅使用第5行。
设计决策:
看起来您正在构建产品目录,使用数据库存储此数据会好得多。不一定非常重要(MySQL,PostgreSQL)它可以像SQLite一样简单。
答案 2 :(得分:0)
所以这基本上是三个部分:(1)你需要遍历目录,(2)读取每个文件的第五行,(3)你需要在冒号后读取该行的部分。
// open the products directory
$fileHandle = opendir('products');
// create an array to store the file data
$files = array();
// create an array to store products
$products = array();
if ($fileHandle) {
// loop through each file
while ( false !== ($singleFile = readdir($fileHandle)) ) {
// use file() to read the lines of the file
$lines = file($singleFile);
// store the fifth line in each file (the array starts at 0)
$productDescription = explode(":",$lines[4]);
$products[] = productDescription[1]; // the bit after the colon
}
}
// this should show you an array of the products
print_r($products);
答案 3 :(得分:0)
答案:http://pastebin.com/WBBQW9wA (底部的原始代码)
这个过程是三倍的。
我的方法记录良好且易于理解。它使用函数来完成流程的特定部分。很容易理解正在发生的事情,同时也非常强大。
完成工作的功能:
<?php
function readAfterColon($string) {
/**
* @param string $string The string you'd like to split on.
*
* @return string The data after the colon. Multiple colons work just fine.
*/
$array = explode(":",$string, 2);
return isset($array[1]) ? $array[1] : '';
}
function readLine($lineNumber, $fileName) {
/**
* @param int $lineNumber The line number you want to read from the file.
* @param string $fileName The string representing the file you wish to open.
*
* @return string The contents of the line in the file.
*/
$file = file($fileName);
return $file[$lineNumber-1];
}
function readLineFrom($lineNumber, $fileNames) {
/**
* @param int $lineNumber The line number you want to read from the file.
* @param string|array $files Either a string for the file you want to open, or an array of filenames.
*
* @return array An associative array of the filename to the contents of the line.
*/
$lines = array();
if(is_array($fileNames)) {
foreach($fileNames as $fileName) {
$lines[$fileName] = readLine($lineNumber, $fileName);
}
} else {
$lines[$fileNames] = readLine($lineNumber, $fileNames);
}
return $lines;
}
function getFileNamesFromDirectory($directory = '.') {
/**
* @param string $directory The path to directory you'd like to get filenames from. Defaults to current directory.
*
* @return array An array of filenames. Empty if there are no files.
*/
$fileNames = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileNames[] = $directory . $file;
}
}
closedir($handle);
}
return $fileNames;
}
?>
示例:
<?php
//include the functions, then:
//get the line from every file
$descriptions = readLineFrom(5, getFileNamesFromDirectory('/var/www/test/'));
//get the contents after the colon
foreach($descriptions as $fileName=>$description) {
$descriptions[$fileName] = readAfterColon($description);
}
//display it
echo "<pre>\n";
print_r($descriptions);
echo "</pre>";
?>