我需要截断字符串并将其重写为数组
我有一个函数,我从数据库中获取数据
$data['about_text_list'] = $this->about_text_model->get_array();
我从数据库中得到这些字段:id, num, header, text, language
我需要strip_tags和截断文本与函数word_limiter
foreach ($data['about_text_list'] as $items)
{
$data['about_text_list']['text'] = word_limiter($items['text'], 100);
$data['about_text_list']['text'] = strip_tags($items['text']);
}
in view I do foreach
<? foreach ($about_text_list as $line) : ?>
<td><?=$line['text']?></td>
<? endforeach; ?>
但是我得到错误,请告诉我如何做正确的事情,这样的
在控制器的循环中,您将限制单词计数,然后将其设置为数组中的值。然后用strip_tags
函数覆盖这个值。您使用两个函数对相同的值,而不是使用改变的值。(我会先去掉标签,然后限制字数。)
你也只是覆盖$data['about_text_list']['text']
值每次迭代。我假设这需要是一个数组的"文本"值?我会用更新的内容创建一个新数组,并将你的$data['about_text_list']
数组与新数组合并。
将循环改为:
$newarray = array();
foreach ($data['about_text_list'] as $key => $value)
{
$item_text = $value['text'];
$altered = strip_tags($item_text);
$newarray[$key]['text'] = word_limiter($altered, 100);
}
$data['about_text_list'] = array_merge($data['about_text_list'], $newarray);
// here, you create a new empty array,
// then loop through the array getting key and value of each item
// then cache the 'text' value in a variable
// then strip the tags from the text key in that item
// then create a new array that mirrors the original array and set
// that to the limited word count
// then, after the loop is finished, merge the original and altered arrays
// the altered array values will override the original values
另外,我不确定你的错误是什么(因为你还没有告诉我们),但请确保你正在加载文本助手,让你访问word_limiter
函数:
$this->load->helper('text');
当然,这一切都取决于你的数组的结构,我猜现在。