Php 数组中的str_替换
我正在为我的一个客户用CodeIgniter构建一个简单的系统。我想使用Php 数组中的str_替换,php,codeigniter,Php,Codeigniter,我正在为我的一个客户用CodeIgniter构建一个简单的系统。我想使用str_replace将像{company_name}这样的标记替换为一个值,但我似乎无法实现这一点 我目前的代码是: <?php $this->db->where('id', '1'); $data = $this->db->get('pages'); $output = array('row' => $data->row()); $this->template->
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函数
如果要替换数组中的所有项,则需要对其进行迭代:
foreach($data->row() as $key=>$value){
$data[$key] = str_replace("{company_name}","Sample company",$value);
}
使用MySQL函数怎么样
如果您只想用一个值替换另一个值,那么您不必为
str\u replace
使用数组。另外,您能否确保数据->行()
包含准确的字符串“{company\u name}
?$data->row()包含什么?它是字符串还是数组?它是数组。它包含数据库中的所有字段。例如,ID:标题、内容、创建数据、编辑日期。但我只在我的视图中使用标题和内容。
foreach($data->row() as $key=>$value){
$data[$key] = str_replace("{company_name}","Sample company",$value);
}
$this->db->select("SELECT REPLACE(field, '{company_name}', 'Sample company') FROM pages ");