如题:怎么删除TSV文件每一行中,""引号里面的换行符?TSV文件里,有多行文件.每行中各个项目是用TAB分开,有的项目被"引号隔开,被引号隔开的项目内有可能会有换行符,怎么删除这个引号中的换行符呢?

解决方案 »

  1.   

    取文件内容,然后用正则替换,如下:(代码未经测试)
    $content = file_get_contents($filename);
    $newContent = preg_replace('/\"(.*)\n(.*)\"/m', '\"$1$2\"', $content);
    file_put_contents($filename, $newContent);
      

  2.   


    能不能解释一下这个正则,我对正则不太了解。根据给给的答案:
    测试结果
    原:www"xaf dsf"fffffffffffffff"abc def"asfddsafd
    转换后:www\"xaf dsf"fffffffffffffff\"abc def"asfddsafd如果改为下面的$content = file_get_contents($filename);
    $newContent = preg_replace('/\"(.*)\r\n(.*)\"/m', '\"$1$2\"', $content);
    file_put_contents($filename, $newContent);
    测试结果
    原:www"xaf dsf"fffffffffffffff"abc def"asfddsafd
    转换后:www\"xafdsf"fffffffffffffff\"abc def"asfddsafd
      

  3.   

    修改后的:
    $newContent = preg_replace('/\"([^"]*)\r\n([^"]*)\"/m', '"$1$2"', $content);正则的用法,参看下面的网页:
    http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
      

  4.   

    测试通过有的系统WIN系统换行符是\r\n,LINUX是\n,Mac 是\r
    怎么才可以都适用呢?$newContent = preg_replace('/\"([^"]*)\r\|\n\|\r\n([^"]*)\"/m', '"$1$2"', $content);这样不可以吗?