Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组中的str_替换_Php_Codeigniter - Fatal编程技术网

Php 数组中的str_替换

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->

我正在为我的一个客户用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函数

如果要替换数组中的所有项,则需要对其进行迭代:

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 ");