[英]Check field and rewrite to array Codeigniter
我需要截斷字符串並將其重寫回數組
我有一個功能,可以從數據庫中獲取數據
$data['about_text_list'] = $this->about_text_model->get_array();
我從數據庫獲得這些字段:id,num,標頭,文本,語言
我需要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']);
}
鑒於我做了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');
當然,這一切都取決於您數組的結構,我現在正在猜測。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.