我正在为我的客户在CodeIgniter中构建一个简单的系统。我想使用str_replace
将{company_name}之类的标记替换为值,但我似乎无法使其工作。
我目前的代码是:
<?php
$this->db->where('id', '1');
$data = $this->db->get('pages');
$output = array('row' => $data->row());
$this->template->load('frontend/template', 'frontend/page', $output);
我的尝试是:
<?php
$this->db->where('id', '1');
$data = $this->db->get('pages');
$output = str_replace(
array("{company_name}"),
array("Sample company"),
$data->row()
);
$this->template->load('frontend/template', 'frontend/page', $output);
但我认为我目前正走错路,因为没有任何表现,我想要一些帮助或解释我在这里做错了什么。谢谢。
答案 0 :(得分:1)
使用MySQL函数REPLACE
怎么样?$this->db->select("SELECT REPLACE(field, '{company_name}', 'Sample company') FROM pages ");
答案 1 :(得分:0)
请查看php str_replace函数。
如果要替换数组中的所有项,则需要迭代它:
foreach($data->row() as $key=>$value){
$data[$key] = str_replace("{company_name}","Sample company",$value);
}