【问题标题】:Line breaks not working in php string [duplicate]换行符在php字符串中不起作用[重复]
【发布时间】:2017-02-28 01:29:59
【问题描述】:

我有以下函数,用于在 PHP 中对样式进行排队,与 WordPress 函数非常相似。

public function printHTML() {
        foreach($this->styles as $style) {
            $style = sprintf('<link href="%s" rel="stylesheet" type="text/css" />\n', $style);
            $this->print_html .= $style;
        }
        return $this->print_html;
}

当在我的 index.php 页面中调用它时,我会执行以下操作:

echo nl2br($styles->printHTML());

但它似乎只是将整个 print_html 放入在源代码中看起来很难看的 1 行中,知道我在这里做错了什么吗?

【问题讨论】:

    标签: php


    【解决方案1】:

    \n 仅适用于双引号字符串,即 echo "\n"; 或您的情况

    $style = sprintf("<link href='%s' rel='stylesheet' type='text/css' />\n", $style);
    

    或者做得更简单

    $this->print_html .= "<link href='$style' rel='stylesheet' type='text/css' />\n";
    

    【讨论】:

    • 你是明星,谢谢!
    猜你喜欢
    • 2014-01-21
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2011-09-07
    • 2017-07-25
    • 2013-05-09
    • 1970-01-01
    相关资源
    最近更新 更多