在PHP中,如何在heredoc(此处为文档)中折叠换行符?
出于CLI目的,我希望在在PHP中,如何在heredoc(此处为文档)中折叠换行符?,php,line-breaks,heredoc,Php,Line Breaks,Heredoc,出于CLI目的,我希望在herdoc(此处为文档)中部分折叠(忽略换行) 目前,我在要折叠的行的末尾使用%%,然后使用str\u 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 co
herdoc
(此处为文档)中部分折叠(忽略换行)
目前,我在要折叠的行的末尾使用%%
,然后使用str\u 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);
有什么想法吗
当前结论(2018/01/17)
禁用换行符作为默认行为,并改用BR
标记换行。
1.将PHP\u EOL
(换行符)替换为“”(空白)。
2.将BR
标记替换为PHP\u 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}
是“不间断的”,而{…}
在模板中很常见,这只是一种观点
但我也会使用regx而不是str_替换
preg_replace('/{nbr}[\r\n]+/', '', $str);
通过这种方式,它可以匹配\r
、\r\n
、\n
甚至\n\n
或旧的Mac、Windows、Linux和多行终端
您可以看到您可以求助于破坏HTML标准,即使用
标记来表示何时需要换行。这会让习惯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;
$string=这会让习惯HTML的人感觉更直观…
我明白了,首先删除所有换行符,然后将BR标记替换为换行符。这是一个好主意,而且很直观!所以没有转义码可以取消换行吗?因为%%看起来太通用了
确实如此。但以前它是%BR%
。但有时有人更改了孔规格,认为是因为他/她懒惰打字。这就是为什么我会有这种不舒服的感觉_(
$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;