PHP file_put_contents 新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6337551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP file_put_contents new line
提问by fish man
I tried use file_put_contents output new page. but I meet some trouble in breaking new line.
我尝试使用 file_put_contents 输出新页面。但我在打破新路线时遇到了一些麻烦。
<?php
$data ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n';
$data .='<html xmlns="http://www.w3.org/1999/xhtml" lang="en">\r\n';
$data .='<head>\r\n';
$data .='</head>\r\n';
$data .='<body>\r\n';
$data .='<p>put something here</p>\r\n';
$data .='</body>\r\n';
$data .='</html>\r\n';
file_put_contents( dirname(__FILE__) . '/new.php', $data);
?>
I tried \n
or \r\n
, they all can not make a new line:
我试过\n
or \r\n
,他们都不能换行:
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml" lang="en">\r\n<head>\r\n</head>\r\n<body>\r\n<p>put something here</p>\r\n</body>\r\n</html>\r\n
回答by Sabeen Malik
Using \r
or \n
in single quotes carries it literally. use double quotes instead like "\r\n"
使用\r
或\n
在单引号中表示字面意思。使用双引号代替像 "\r\n"
So one line might become:
所以一行可能会变成:
$data .= "<head>\r\n";
or
或者
$data .='<head>' . "\r\n";
回答by Zecc
You are using single-quoted character literals, which don't interpret escape sequences.
Either switch to double-quoted strings or, preferably, use heredoc syntax.
您正在使用单引号字符文字,它不解释转义序列。
要么切换到双引号字符串,要么最好使用heredoc 语法。
<?php
$data = <<<CONTENTS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
</head>
<body>
<p>put something here</p>
</body>
</html>
CONTENTS;
file_put_contents( dirname(__FILE__) . '/new.php', $data);
?>
But really, why are you writing a hard-coded file? That's really strange.
但说真的,你为什么要写一个硬编码的文件?这真的很奇怪。