如何在不使用正则表达式的情况下从txt文件中删除


How to delete from txt file without using regular expression?

输入:从文本文件while ($data = FGets($soubor)) { ... }读取输入。

输出:我希望在没有'n 't的情况下将内容安全到DB(导入)。

我认为,它为修剪提供了一种替代方案:

while () ...
$editedText = trim($data, "'t");

我需要''t,''n并签入整个txt文档。有什么建议和样品吗?

在没有RE的情况下,完成这项简单任务的最佳方法是什么

str_replace()函数是您的新好友。


$editedText = str_replace(array("'t", "'n"), "", $data);
  • strtr:http://www.php.net/manual/en/function.strtr.php

    strtr($s,array("''r"=>","''n"=>"","''t"=>"));

  • str_replace:http://www.php.net/manual/en/function.str-replace.php

    参见其他答案

我使用

 s.trim();

它有助于从字符串两端丢失whiteSpace、''n、''t等。