针对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;