换行线已经删除了吗?

时间:2012-01-22 04:30:42

标签: php string

我正在使用phpclasses.org的CSV脚本。它从表/更多表中检索列名和列值,并创建CSV。

有一件事我不明白。

以下是我正在查看的代码:

function createcsv($tablename){

    $rs  = $this->SelectAll($tablename);
    $rs1 = $this->SelectAll($tablename);
    if($rs){
        $string ="";
        /// Get the field names
        $fields =  mysql_fetch_assoc($rs1);
        if(!is_array($fields))
          return;
        while(list($key,$val) =each($fields)) {
            $string .= $key.',';
         }
        $string = substr($string,0,-1)."\015\012"; //removes last and comma and adds a newline
        /// Get the data
        while($row = mysql_fetch_assoc($rs)) {
            while(list($key,$val) =each($row)){
              $row[$key] = strip_tags(html_entity_decode($row[$key])); //strips tangs from the html decoded value
              $row[$key] = str_replace(',',' ',rtrim($row[$key])); //replaces commas with empty spaces from the trimmed value
              $row[$key] = str_replace("\015\012",' ',$row[$key]); 
            }
            $string .= (implode($row,","))."\015\012";
         }
            echo $string;

        //$fp = fopen($this->path.$tablename.".csv",'w');
        //fwrite($fp,$string);
        //fclose($fp);
    }

}

我想知道的两条线是:

$row[$key] = str_replace(',',' ',rtrim($row[$key])); //replaces commas with empty spaces from the trimmed value
$row[$key] = str_replace("\015\012",' ',$row[$key]); 

我认为rtrim也删除了新行(\ n)...那么为什么使用第二行$row[$key] = str_replace("\015\012",' ',$row[$key]);

2 个答案:

答案 0 :(得分:2)

rtrim从字符串的 end (r =“right”)中删除换行符。您引用的行会删除字符串中的任何位置。

答案 1 :(得分:2)

查看您在问题中关联的页面,我可以阅读

  

来自字符串的结尾

所以,我可以得出结论,它不会从字符串的任何其他部分删除任何新行。