优雅的换行解决方案 (PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2726855/
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
Elegant solution for line-breaks (PHP)
提问by eozzy
$var = "Hi there"."<br/>"."Welcome to my website"."<br/>;"
echo $var;
Is there an elegant way to handle line-breaks in PHP? I'm not sure about other languages, but C++ has eolso something thats more readable and elegant to use?
有没有一种优雅的方法来处理 PHP 中的换行符?我不确定其他语言,但是 C++ 有eol一些更易读和更优雅的东西吗?
Thanks
谢谢
采纳答案by eozzy
I ended up writing a function that has worked for me well so far:
我最终编写了一个到目前为止对我有用的函数:
// pretty print data
function out($data, $label = NULL) {
$CLI = (php_sapi_name() === 'cli') ? 'cli' : '';
$gettype = gettype($data);
if (isset($label)) {
if ($CLI) { $label = $label . ': '; }
else { $label = '<b>'.$label.'</b>: '; }
}
if ($gettype == 'string' || $gettype == 'integer' || $gettype == 'double' || $gettype == 'boolean') {
if ($CLI) { echo $label . $data . "\n"; }
else { echo $label . $data . "<br/>"; }
}
else {
if ($CLI) { echo $label . print_r($data,1) . "\n"; }
else { echo $label . "<pre>".print_r($data,1)."</pre>"; }
}
}
// Usage
out('Hello world!');
$var = 'Hello Stackoverflow!';
out($var, 'Label');
回答by Pascal MARTIN
For linebreaks, PHP as "\n"(see double quote strings)and PHP_EOL.
对于换行符,PHP as (参见双引号字符串)和."\n"PHP_EOL
Here, you are using <br />, which is not a PHP line-break : it's an HTML linebreak.
在这里,您使用的是<br />,这不是 PHP 换行符:它是 HTML 换行符。
Here, you can simplify what you posted (with HTML linebreaks): no need for the strings concatenations : you can put everything in just one string, like this :
在这里,您可以简化您发布的内容(使用 HTML 换行符):不需要字符串连接:您可以将所有内容都放在一个字符串中,如下所示:
$var = "Hi there<br/>Welcome to my website<br/>";
Or, using PHP linebreaks :
或者,使用 PHP 换行符:
$var = "Hi there\nWelcome to my website\n";
Note : you might also want to take a look at the nl2br()function, which inserts <br>before \n.
注意 :您可能还想看看在之前nl2br()插入的函数。<br>\n
回答by TheVyom
I have defined this:
我已经定义了这个:
if (PHP_SAPI === 'cli')
{
define( "LNBR", PHP_EOL);
}
else
{
define( "LNBR", "<BR/>");
}
After this use LNBRwherever I want to use \n.
在此使用之后,LNBR我想在任何地方使用\n.
回答by Kolitha Warnakulasooriya
in php line breaks we can use PHP_EOL (END of LINE) .it working as "\n" but it cannot be shown on the ht ml page .because we have to give HTML break to break the Line..
在 php 换行符中,我们可以使用 PHP_EOL (END of LINE) 。它作为“\n”工作,但它不能显示在 html 页面上。因为我们必须给 HTML 换行符才能换行..
so you can use it using define
所以你可以使用define来使用它
define ("EOL","<br>");
then you can call it
然后你可以调用它
回答by RandyMorris
Not very "elegant" and kinda a waste, but if you really care what the code looks like you could make your own fancy flag and then do a str_replace.
不是很“优雅”,有点浪费,但如果你真的关心代码的样子,你可以制作自己的花哨的标志,然后做一个 str_replace。
Example:<br />
$myoutput = "After this sentence there is a line break.<b>.|..</b> Here is a new line.";<br />
$myoutput = str_replace(".|..","<br />",$myoutput);<br />
or
或者
how about:<br />
$myoutput = "After this sentence there is a line break.<b>E(*)3</b> Here is a new line.";<br />
$myoutput = str_replace("E(*)3","<br />",$myoutput);<br />
I call the first method "middle finger style" and the second "goatse style".
我称第一种方法为“中指式”,第二种方法为“山羊式”。
回答by Sarfraz
Because you are outputting to the browser, you have to use <br/>. Otherwise there is \nand \ror both combined.
因为要输出到浏览器,所以必须使用<br/>. 否则有\n和\r或两者结合。
回答by mkgrunder
Well, as with any language there are several ways to do it.
好吧,与任何语言一样,有几种方法可以做到。
As previous answerers have mentioned, "<br/>"is not a linebreak in the traditional sense, it's an HTML line break. I don't know of a built in PHP constant for this, but you can always define your own:
正如之前的回答者所提到的, "<br/>"不是传统意义上的换行符,它是 HTML 换行符。我不知道为此内置 PHP 常量,但您始终可以定义自己的:
// Something like this, but call it whatever you like
const HTML_LINEBREAK = "<br/>";
If you're outputting a bunch of lines (from an array of strings for example), you can use it this way:
如果您要输出一堆行(例如从字符串数组),您可以这样使用它:
// Output an array of strings
$myStrings = Array('Line1','Line2','Line3');
echo implode(HTML_LINEBREAK,$myStrings);
However, generally speaking I would say avoid hard coding HTML inside your PHP echo/print statements. If you can keep the HTML outside of the code, it makes things much more flexible and maintainable in the long run.
但是,一般来说,我会说避免在 PHP 回显/打印语句中对 HTML 进行硬编码。如果您可以将 HTML 保留在代码之外,那么从长远来看,它会使事情变得更加灵活和可维护。
回答by Hamish Ahern
\n didn't work for me. the \n appear in the bodytext of the email I was sending.. this is how I resolved it.
\n 对我不起作用。\n 出现在我发送的电子邮件的正文中。我就是这样解决的。
str_pad($input, 990); //so that the spaces will pad out to the 990 cut off.
str_pad($input, 990); //以便空格将填充到 990 截止。

