【问题标题】:php read write file not saving leading whitespacephp读写文件不保存前导空格
【发布时间】:2015-08-16 07:38:04
【问题描述】:

我正在处理一个长约 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

【问题讨论】:

    标签: php file fwrite fgets fputcsv


    【解决方案1】:

    如果你想要空格,因为它在下面的行修改

    $line = trim(fgets($access));
    

    进入

    $line = fgets($access);
    

    这应该可以解决您的问题。

    【讨论】:

    • 这就是我来这里的原因。深夜的虫子。感谢您的帮助。
    • @user3314053 希望它能解决您的问题。享受编码:-)
    • @user3314053 如果解决了就接受meta.stackexchange.com/questions/5234/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2013-06-10
    • 1970-01-01
    相关资源
    最近更新 更多