php无法解释导出函数的转义字符

时间:2013-04-19 09:56:30

标签: php string

我的php服务器无法将“\ n”解释为(LF或CR)。我的php.ini出了什么问题?

这里是一个示例代码:

<?php echo "hell o boys \n hell o girls \t hell o gays"; ?>

只是打印:

hell o boys hell o girls hell o gays

我希望在哪里

hell o boys 
 hell o girls      hell o gays

需要帮助。真的很烦恼这样的小事。 我不想使用正则表达式。

在这种情况下,我想在下面使用此函数,但由于escape char prolem它将无法工作。 这里:

<?php
function export_excel_csv()
{
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db("database",$conn);

    $sql = "SELECT * FROM table";
    $rec = mysql_query($sql) or die (mysql_error());

    $num_fields = mysql_num_fields($rec);

    for($i = 0; $i < $num_fields; $i++ )
    {
        $header .= mysql_field_name($rec,$i)."\\t";
    }

    while($row = mysql_fetch_row($rec))
    {
        $line = '';
        foreach($row as $value)
        {                                            
            if((!isset($value)) || ($value == ""))
            {
                $value = "\\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\\t";
            }
            $line .= $value;
        }
        $data .= trim( $line ) . "\\n";
    }

    $data = str_replace("\\r" , "" , $data);

    if ($data == "")
    {
        $data = "\\n No Record Found!\n";                        
    }

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=reports.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\\n$data";
}
?>

2 个答案:

答案 0 :(得分:3)

使用

nl2br

替换为\ n的换行功能。喜欢这个

$str = "hell o boys \n hell o girls \t hell o gays";

echo nl2br($str);

答案 1 :(得分:0)

$a = "hell o boys \n hell o girls \t hell o gays";    
$a=str_replace('\n',PHP_EOL,$a);
echo $a;

如果你想在html中显示代码。 尝试

echo "<pre>{$a}</pre>";