在 PHP 中,如何折叠 heredoc(此处为文档)中的换行符?
In PHP how can I collapse the line breaks inside heredoc (here document)?
出于 CLI 目的,我想 部分 折叠(忽略换行符)在 heredoc
(此处文档)内。
目前,我在要折叠的行末尾使用 %%
,然后使用 str_replace("%%\n",'', $string);
替换它们。但是我觉得不太舒服。
有没有逃生绳或更聪明的方法?
例如:
<?php
$string=<<<EOL
This is a single long line, and it has to be
in one line, though the SideCI thing (as a
coding regulation) I have to line break.
But this line stays short.
And this line too.
And the backslash such as \
won't work. Nor /
won't work.
My alternative way is to use %%
strings and replace them later.
EOL;
$string .= 'And I don\'t want to do this ';
$string .= 'to merge strings.';
echo str_replace("%%\n",'', $string);
我得到如下:
This is a single long line, and it has to be
in one line, though the SideCI thing (as a
coding regulation) I have to line break.
But this line stays short.
And this line too.
And the backslash such as \
won't work. Nor /
won't work.
My alternative way is to use strings and replace them later.
And I don't want to do this to merge strings.
有什么想法吗?
当前结论(2018/01/17)
Disable line breaks as a default behavior, and use BR
tags to line break instead.
1. Replace PHP_EOL
(line breaks) to ''(blank).
2. Replace the BR
tags to PHP_EOL
.
示例代码:
<?php
$string=<<<EOL
This is a single long line, and it has to be
in one line, though the SideCI thing (as a
coding regulation) I have to line break.<br>
But this line stays short.<br>
And this line too.<br>
My post alternative way was to use %%
chars and replace them later.<br>
EOL;
$string = str_replace(PHP_EOL,'', $string);
$string = str_ireplace(["<br />","<br>","<br/>"], PHP_EOL, $string);
echo $string;
我个人会使用 {nbr}
之类的东西,因为 %%
看起来太笼统了,其中 {nbr}
是 "no break" 而 {...}
在模板中很常见,那只是一个意见。
但我也会使用 regx 而不是 str_replace
preg_replace('/{nbr}[\r\n]+/', '', $str);
这样它匹配 \r
、\r\n
、\n
甚至 \n\n
或旧的 Mac、Windows、Linux 和多行结尾。
可以看到it here:
您可以诉诸修改 HTML 标准,即使用 <br>
标记来说明您何时需要换行符。对于习惯 HTML...
的人来说,这会感觉更直观
$string=<<<EOL
This is a single long line, and it has to be
in one line, though the SideCI thing (as a
coding regulation) I have to line break.
But this line stays short.
And this line too.
And the backslash such as \<br>
won't work. Nor /<br>
won't work.<br>
My alternative way is to use %%<br>
strings and replace them later.
EOL;
$string = str_replace(PHP_EOL,'', $string);
$string = str_ireplace(["<br />","<br>","<br/>"], PHP_EOL, $string);
echo $string;
请注意使用 PHP_EOL
以使用新 line/line 中断或您使用的平台具有的任何组合的正确当前编码。
出于 CLI 目的,我想 部分 折叠(忽略换行符)在 heredoc
(此处文档)内。
目前,我在要折叠的行末尾使用 %%
,然后使用 str_replace("%%\n",'', $string);
替换它们。但是我觉得不太舒服。
有没有逃生绳或更聪明的方法?
例如:
<?php
$string=<<<EOL
This is a single long line, and it has to be
in one line, though the SideCI thing (as a
coding regulation) I have to line break.
But this line stays short.
And this line too.
And the backslash such as \
won't work. Nor /
won't work.
My alternative way is to use %%
strings and replace them later.
EOL;
$string .= 'And I don\'t want to do this ';
$string .= 'to merge strings.';
echo str_replace("%%\n",'', $string);
我得到如下:
This is a single long line, and it has to be
in one line, though the SideCI thing (as a
coding regulation) I have to line break.
But this line stays short.
And this line too.
And the backslash such as \
won't work. Nor /
won't work.
My alternative way is to use strings and replace them later.
And I don't want to do this to merge strings.
有什么想法吗?
当前结论(2018/01/17)
Disable line breaks as a default behavior, and use
BR
tags to line break instead.
1. ReplacePHP_EOL
(line breaks) to ''(blank).
2. Replace theBR
tags toPHP_EOL
.
示例代码:
<?php
$string=<<<EOL
This is a single long line, and it has to be
in one line, though the SideCI thing (as a
coding regulation) I have to line break.<br>
But this line stays short.<br>
And this line too.<br>
My post alternative way was to use %%
chars and replace them later.<br>
EOL;
$string = str_replace(PHP_EOL,'', $string);
$string = str_ireplace(["<br />","<br>","<br/>"], PHP_EOL, $string);
echo $string;
我个人会使用 {nbr}
之类的东西,因为 %%
看起来太笼统了,其中 {nbr}
是 "no break" 而 {...}
在模板中很常见,那只是一个意见。
但我也会使用 regx 而不是 str_replace
preg_replace('/{nbr}[\r\n]+/', '', $str);
这样它匹配 \r
、\r\n
、\n
甚至 \n\n
或旧的 Mac、Windows、Linux 和多行结尾。
可以看到it here:
您可以诉诸修改 HTML 标准,即使用 <br>
标记来说明您何时需要换行符。对于习惯 HTML...
$string=<<<EOL
This is a single long line, and it has to be
in one line, though the SideCI thing (as a
coding regulation) I have to line break.
But this line stays short.
And this line too.
And the backslash such as \<br>
won't work. Nor /<br>
won't work.<br>
My alternative way is to use %%<br>
strings and replace them later.
EOL;
$string = str_replace(PHP_EOL,'', $string);
$string = str_ireplace(["<br />","<br>","<br/>"], PHP_EOL, $string);
echo $string;
请注意使用 PHP_EOL
以使用新 line/line 中断或您使用的平台具有的任何组合的正确当前编码。