php读写文件不保存前导空格

时间:2015-08-16 07:38:05

标签: php file fwrite fgets fputcsv

我正在处理一个大约2000行的文件。它是一个简单的读取,替换文本和回写,然而,领先的空白没有被保留。

有什么想法吗?

    <?php
    $access = fopen("oldfile.txt", "r");
    $y=9999;
    for ($i=1; $i<=$y; $i++)
        {
        $line = trim(fgets($access));
        $loc = strpos($line, "findtext", 0);
        if ($loc)
            {
            $loc = $loc +6;
            $end =  strpos($line, "endtext", 0);
            $pull = substr($line, $loc, ($end-$loc));

            $loc = strpos($line, 'foundthis', 0);
            $end = $loc +12;

            $newline = substr($line, 0, $loc).'foundthis'.$pull.'" '.
     substr($line, $end);

            }
        else
            { $newline = $line;  }

        file_put_contents("newfile.txt", $newline."\r\n", FILE_APPEND);

        }
    fclose($access);

 ?>

oldfile.txt
    labelinput  "Adhesives" name="MyName"Adhesiveslabel
    labelinput  "Cord, Yarn & Material" name="MyName"Cord, Yarn & 
        labelinput  "Corners" name="MyName"Cornerslabel
            labelinput  "Ink & Ink Pads" name="MyName"Ink & Ink Padslabel

newfile.txt
labelinput  "Adhesives" name="MyName"Adhesiveslabel
labelinput  "Cord, Yarn & Material" name="MyName"Cord, Yarn & Materiallabel
labelinput  "Corners" name="MyName"Cornerslabel
labelinput  "Ink & Ink Pads" name="MyName"Ink & Ink Padslabel

0 个答案:

没有答案