You could do it in the controller:
class Something extends CI_Controller{
public function index(){
$data['mytext'] = $this->_truncate('some text to truncate');
//truncated text will be available in the view as $mytext
$this->load->view('some_view', $data);
}
private function _truncate($text = NULL){
if($text){
$chars = 100;
$mytext = substr($text,0,$chars);
$mytext = substr($text,0,strrpos($text,' '));
return $mytext;
}
}
}
Edit:
You are calling db stuff in your view which is entirely not Codeigniter MVC.
This is what it might look like in MVC:
Controller
class Something extends CI_Controller{
public function index(){
$test_text = $this->my_model->get_text();
$data['test_text'] = $this->_truncate($test_text);
//truncated text will be available in the view as $test_text
$this->load->view('some_view', $data);
}
private function _truncate($text = NULL){
if($text){
$chars = 100;
$mytext = substr($mytext,0,$chars);
$mytext = substr($mytext,0,strrpos($mytext,' '));
return $mytext;
}
}
}
Model
class My_Model extends CI_Model{
public function get_text(){
//$this->db->get_where....or other db functions
return "testing text... this is only a test";
}
}
View
<html>
<body>
<b><?php echo $test_text; ?></b>
</body>