【问题标题】:In PHP how can I collapse the line breaks inside heredoc (here document)?在 PHP 中,如何折叠 heredoc(此处文档)中的换行符?
【发布时间】: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


    【解决方案1】:

    您可以通过修改使用&lt;br&gt; 标记的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;
    

    注意使用PHP_EOL 来使用新行/换行符的正确当前编码或您使用的平台所具有的任何组合。

    【讨论】:

    • This will feel more intuitive for people used to HTML... 我明白了,先删除所有换行符,然后将 BR 标签替换为换行符。这是一个巧妙的想法,而且很直观!那么没有转义码可以取消换行符吗?
    【解决方案2】:

    我个人会使用{nbr} 之类的东西,只是因为%% 似乎太通用了,其中{nbr} 是“不中断”,而{...} 在模板中很常见,这只是一种意见。

    但我也会使用 regx 而不是 str_replace

    preg_replace('/{nbr}[\r\n]+/', '', $str);
    

    这样它匹配 \r\r\n\n 甚至 \n\n 或旧 Mac、Windows、Linux 和多行结尾。

    你可以看到it here:

    【讨论】:

    • because %% seems too generic 确实如此。不过之前它曾经是%BR%。但有时有人更改了孔规格,认为是因为他/她懒惰打字。这就是我想出这种不舒服的原因。 :_(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 2016-08-24
    相关资源
    最近更新 更多