从txt文件的每一行创建新的txt文件

时间:2018-02-24 23:46:41

标签: php file-io

我有各种txt文件,如ex:dictionary,每个新行都是term,然后是description。管道划界。

我编写了一个简单的脚本,实际上读取每一行,然后创建(fwrite)以术语和描述命名的新txt文件作为该文件的文本。

它有效,但我想知道是否有更好的方法,考虑到特殊字符,可能缓冲,不确定从哪里开始。

$file = fopen("test.txt", "r") or exit("Unable to open file!");
//  Output a line of the file until the end is reached
while(!feof($file))
{
    $line = fgets($file);

    // making all lowercase - optional
    $line = strtolower($line);

    // take the first value before delimiter
    $var = substr( $line, 0, strpos( $line, '|' ) );

    // remove some characters - optional ( depends on a file structure and contents )
    $var = str_replace("-", "", $var);

    // what txt should be written into a each new file
    $txt = str_replace("|", "", $line);

    // name the file 
    $myfile = fopen("$var.txt", "w") or die("Unable to open file!");

    // write
    fwrite($myfile, $txt);

    //close each
    fclose($myfile);

}

//close
fclose($file);

更新

$file = fopen("test.txt", "r") or exit("Unable to open file!");
//  Output a line of the file until the end is reached
while(!feof($file))
    {
        $line = fgets($file);

        // @sorak fix
        $line = fgets($file);
        $name = preg_replace('/[^0-9a-zA-Z]/', '', 
          explode('|', $line)[0]);

        // making it lowercase - optional for each
        $line = strtolower($line);
        $name = strtolower($name);

        // what txt should be written into a each new file
        $txt = str_replace("|", " ", $line);

        // name the file 
        $myfile = fopen("$name.txt", "w") or die("Unable to open file!");

        // write
        fwrite($myfile, $txt);

        // action echo
        echo "$myfile - $name - $txt </br>";

        //close each
        fclose($myfile);

    }

//close
fclose($file);

由于上次更新包含一些重复的代码行/错误,导致每隔一行跳过:)我发布了新的固定和位升级版本。

// this utility is for creating multiple names.txt files from separate lines in original.txt file
// format for original file is: is name|text

// increase memory limit to 32M
ini_set('memory_limit','32M');

// increase 1440 seconds = 24 minutes
ini_set('max_execution_time', 1440);

$file = fopen("original.txt", "r");
if ($file) {
    while (($line = fgets($file)) !== false) {

        // @sorak fix
        //$line = fgets($file);
        $name = preg_replace('/[^0-9a-zA-Z]/', '', 
          explode('|', $line)[0]);

        // making it lowercase - optional for each
        $line = strtolower($line);
        $name = strtolower($name);
        $filename = $name . ".txt";

        // what text should be written into a each new file
        // change pipe separator if needed
        $txt = str_replace("|", " ", $line);

        // set values 
        $myfile = fopen($filename, "a") or die("Unable to open file!");
        $dir = "/example.com/could_be_dynamic_folder_name";

        // write
        fwrite($myfile, $txt);
        // chmod($dir,0777); optional if creation of dir is from GET/other value and not in the same parent

        // action echo
        echo "$myfile </br>";
        echo "$dir/$filename </br>";
        echo "$txt </br></br>";

        // chmod
        chmod($dir,0777);
        chmod("$dir/$filename",0666); // remember to set this script to 0666 

    }

    fclose($file);

} else {

    // error echo
    echo "something went wrong, error";

} 

现在就像魅力一样。案件结案。

0 个答案:

没有答案