Php 如何在codeigniter中调用我的库函数?
我已经创建了一个库和一个为修剪和显示半字符定义的函数:Php 如何在codeigniter中调用我的库函数?,php,html,codeigniter-3,Php,Html,Codeigniter 3,我已经创建了一个库和一个为修剪和显示半字符定义的函数: class Strlen_trim { function trim_text($input, $length, $ellipses = true, $strip_html = true) { //strip tags, if desired if ($strip_html) { $input = strip_tags($input); } //no need to trim, already
class Strlen_trim {
function trim_text($input, $length, $ellipses = true, $strip_html = true) {
//strip tags, if desired
if ($strip_html) {
$input = strip_tags($input);
}
//no need to trim, already shorter than trim length
if (strlen($input) <= $length) {
return $input;
}
//find last space within length
$last_space = strrpos(substr($input, 0, $length), ' ');
$trimmed_text = substr($input, 0, $last_space);
//add ellipses (...)
if ($ellipses) {
$trimmed_text .= '...';
}
return $trimmed_text;
}
}
视图侧
<?php echo trim_text($widgets['content'],15); ?>
在视图端,直接调用函数就是错误所在 你可以这样做 方式1: 控制器端:
$this->load->library('Strlen_trim');
$data = array();
$data['widget_content'] = $this->Strlen_trim->trim_text($widgets['content'],15);
$this->load->view('view_name',$data);
$this->load->library('Strlen_trim');
视图侧:
方式2:
控制器端:
$this->load->library('Strlen_trim');
$data = array();
$data['widget_content'] = $this->Strlen_trim->trim_text($widgets['content'],15);
$this->load->view('view_name',$data);
$this->load->library('Strlen_trim');
视图侧:
我建议使用CI内置文本助手,而不是为此创建自定义库
在autoload.php中加载文本帮助程序,如下所示:
$autoload['helper'] = array('text');
在您看来,使用字符限制器或单词限制器,无论您想要什么,如下所示:
// with character_limiter()
<?php echo character_limiter($widgets['content'],15); ?>
// with word_limiter()
<?php echo word_limiter($widgets['content'],5); ?>
更多信息:您遇到了什么错误。我从视图侧得到了对未定义函数trim_text的错误调用,要调用视图中的函数,请使用$this->libraryname->functionMeardGuments;我试过这个$this->Strlen_trim->trim_text$widgets['content'],15;鉴于side@vivekanandgangesh,我建议您不要使用自定义库,而是使用CI文本帮助器。您是否检查我的答案是否有用