PHP:在文本文件中的每个$ num字后插入换行符

时间:2015-06-08 18:20:34

标签: php text-files

我想清理一个.txt文件,通过在每个第四个单词后插入一个换行符使其更具可读性。该文件已填充数组中的数据。

这是我到目前为止看到的thisthis

<?php
require "simple_html_dom.php";

$html = file_get_html("http://www.lipsum.com/");

$data = array();
$counter = 0;


foreach($html->find("div") as $tr){
    $row = array();
    foreach($tr->find("div") as $td){
        $row[] = $td->plaintext;
    }
     $data[] = $row;

    }


ob_start();
var_dump($data);

$data = preg_replace('~((\w+\s){4})~', '$1' . "\n", $data);
file_put_contents('new_text_file.txt', $data);


//$handlefile = fopen("newfile.txt", "w") or die("Unable to open file!");
//file_put_contents('newfile.txt', $data);
//$output = ob_get_clean();
//$outputFile = "newfile.txt";


//fwrite($handlefile, $output);
//fclose($handlefile);



?>

正如您所看到的,我创建了一个for循环和两个if语句来“计算”单词之间的间距,当counter变量达到3时,插入一个换行符。但它不起作用,因为首先没有来自网站的数据打印到文本文件。但是,如果我删除for循环和if语句,而不进行排序。 任何类型的帮助都是适用的!

编辑:更新的代码。

编辑2:最终工作版本。原始问题留待进一步参考。

这是最终的工作版本:

<?php
require "simple_html_dom.php";

$html = file_get_html("http://www.lipsum.com/");
$data = array();
$counter = 0;


foreach($html->find("div") as $tr){
    $row = array();
    foreach($tr->find("div") as $td){
        $row[] = $td->plaintext;
    }
     $data[] = $row;

    }


ob_start();
var_dump($data);
$handlefile = fopen("newfile.txt", "w") or die("Unable to open file!");
file_put_contents('newfile.txt', $data);
$output = ob_get_clean();
$outputFile = "newfile.txt";
fwrite($handlefile, $output);
fclose($handlefile);

function MakeFileReadable($source , $export) {

    $content = file_get_contents($source);
    $x = explode (" " , $content);

    $newx = "";
    $count = 1;
    foreach ($x as $word) {
        $newx .= $word . " ";
        if ($count %12 ==0) $newx .= "\r\n";
        $count ++;
    }

    $fp = fopen("file-export.txt" , 'w');
    if (!$fp) die("There is a problem with opening file...");
    fwrite($fp , $newx);
    fclose($fp);

}

MakeFileReadable("newfile.txt" , "file-export.txt");

?>

3 个答案:

答案 0 :(得分:4)

使用一些数组函数

$string = "one two three four five six seven eight nine ten eleven tweleve thirteen fourteen";

$arr = explode (" " , $string);
$lines = array_chunk($arr,4);
foreach($lines as $line) 
   echo implode (" ", $line)."\r\n";

结果

one two three four
five six seven eight
nine ten eleven tweleve
thirteen fourteen

答案 1 :(得分:3)

这是一个正则表达式解决方案。

<?php
$test = 'one two three four five six seven eight nine ten eleven tweleve thirteen fourteen';
echo preg_replace('~((\w+\s){4})~', '$1' . "\n", $test);

输出:

one two three four 
five six seven eight 
nine ten eleven tweleve

\w是单词字符(字母数字字符加“_”,http://en.wikipedia.org/wiki/Regular_expression),如果您只想A-Z使用[a-z]并使用i修饰符使其不区分大小写,~((\w+\s){4})~i\s是一个空格,{4}需要4次出现\w+\s

根据你的代码...

$data = preg_replace('~((\w+\s){4})~', '$1' . "\n", $data);
file_put_contents('new_text_file.txt', $data);

http://php.net/manual/en/function.file-put-contents.php

每个更新的代码:

<?php
require "simple_html_dom.php";
$html = file_get_html("http://www.lipsum.com/");
$data = array();
$counter = 0;
foreach($html->find("div") as $tr){
    $row = array();
    foreach($tr->find("div") as $td){
        $row[] = $td->plaintext;
    }
    $data[] = $row;
}
$data = preg_replace('~((\w+\s){4})~', '$1' . "\n", implode(' ', $data));
file_put_contents('new_text_file.txt', $data, FILE_APPEND | LOCK_EX);

答案 2 :(得分:1)

最后测试的代码:

    <?php
function MakeFileReadable($source , $export) {

    $content = file_get_contents($source);
    $x = explode (" " , $content);

    sort($x);
    $newx = "";
    $count = 1;
    foreach ($x as $word) {
        $newx .= $word . " ";
        if ($count %4 ==0) $newx .= "\r\n";
        $count ++;
    }

    $fp = fopen($export , 'w');
    if (!$fp) die("There is a problem with opening file...");
    fwrite($fp , $newx);
    fclose($fp);

}

MakeFileReadable("file-input.txt" , "file-export.txt");