优雅的换行解决方案(PHP)
$var = "Hi there"."<br/>"."Welcome to my website"."<br/>;"
echo $var;
PHP 中有没有一种优雅的方式来处理换行符?我不确定其他语言,但 C++ 有 eol
所以使用起来更具可读性和优雅性?
谢谢
$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 eol
so something thats more readable and elegant to use?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于换行符,PHP 为
"\n"
(请参阅 双引号字符串) 和PHP_EOL
。在这里,您使用的是
,它不是 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 :
或者,使用 PHP 换行符:
注意:您可能还想看看
nl2br( )
函数,在\n
之前插入
。For linebreaks, PHP as
"\n"
(see double quote strings) andPHP_EOL
.Here, you are using
<br />
, which is not a PHP line-break : it's an HTML linebreak.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 :
Or, using PHP linebreaks :
Note : you might also want to take a look at the
nl2br()
function, which inserts<br>
before\n
.我已经定义了:
此后,无论我想使用
\n
,都使用LNBR
。I have defined this:
After this use
LNBR
wherever I want to use\n
.在 php 换行符中,我们可以使用 PHP_EOL (END of LINE) 。它作为“\n”
但它不能显示在 html 页面上。因为我们必须给 HTML 中断来中断行。
所以你可以使用定义来使用它
,然后你可以调用它
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..
so you can use it using define
then you can call it
我最终编写了一个到目前为止对我来说效果很好的函数:
I ended up writing a function that has worked for me well so far:
不是很“优雅”并且有点浪费,但是如果您真的关心代码的样子,您可以制作自己的花哨标志,然后执行 str_replace。
或者
我将第一种方法称为“中指风格”,第二种方法为“山羊风格”。
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.
or
I call the first method "middle finger style" and the second "goatse style".
因为您要输出到浏览器,所以必须使用
。否则有\n
和\r
或两者组合。Because you are outputting to the browser, you have to use
<br/>
. Otherwise there is\n
and\r
or both combined.嗯,与任何语言一样,有多种方法可以做到这一点。
正如之前的回答者所提到的,“
不是传统意义上的换行符,它是 HTML 换行符。我不知道是否有内置的 PHP 常量,但您始终可以定义自己的常量:”
如果您要输出一堆行(例如,从字符串数组),您可以这样使用它:
但是,一般来说,我会说避免在 PHP echo/print 语句中对 HTML 进行硬编码。如果您可以将 HTML 保留在代码之外,从长远来看,它会使事情变得更加灵活和可维护。
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:If you're outputting a bunch of lines (from an array of strings for example), you can use it this way:
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.
\n 对我不起作用。 \n 出现在我发送的电子邮件的正文中。这就是我解决它的方法。
str_pad($输入, 990); //这样空格就会填充到 990 处。
\n didn't work for me. the \n appear in the bodytext of the email I was sending.. this is how I resolved it.
str_pad($input, 990); //so that the spaces will pad out to the 990 cut off.