【发布时间】:2018-06-25 23:51:02
【问题描述】:
出于 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)
禁用换行符作为默认行为,并使用
BR标记代替换行符。
1. 将PHP_EOL(换行符)替换为''(空白)。
2.将BR标签替换为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;
【问题讨论】:
标签: php line-breaks heredoc