在变量中添加html

时间:2015-09-03 06:37:36

标签: php cakephp

我尝试在变量中添加html标记<br>(标记如下),但在呈现页面时,它被识别为纯文本。我不确定我哪里出错了。

P.S。不确定是否重要:我使用Cakephp。它是自定义助手类中的变量。

App::uses('AppHelper', 'View/Helper');

class ComaHelper extends AppHelper {
    public $helpers = array('Time');

    public function coma($array, $name) {
        $prefix = ''; 
        $result = '';
        foreach ($array as $key => $value) { 

            $result .= $prefix . $value;

            if ($name == 'condition') { 
                $prefix = ', <br>'; // it's shown as plain text on the page
            } else { 
                $prefix = ', '; 
            }
        } 
        return $result;
    } 
}

页面:

<?php echo h($this->Coma->coma($post['Condition'], 'condition')); ?>

1 个答案:

答案 0 :(得分:5)

问题在于您使用的h()功能:

此功能是htmlspecialchars

的便捷方法

http://api.cakephp.org/2.3/function-h.html

检查结果:

<?= h('<br>') ?>;