如何在codeigniter中调用我的库函数?

时间:2018-07-21 04:57:03

标签: 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 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;
  }
 } 

控制器端

$this->load->library('Strlen_trim');
$this->Strlen_trim->trim_text();

查看侧

<?php echo trim_text($widgets['content'],15); ?>

2 个答案:

答案 0 :(得分:1)

在视图方面,直接调用函数是错误的。

您可以这样做。

  

方法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); 
     

查看侧:

     

<?php echo $widget_content; ?>

     

方法2:

     

控制器端:

$this->load->library('Strlen_trim'); 
     

查看侧:

     

<?php echo $this->Strlen_trim->trim_text($widgets['content'],15); ?>

答案 1 :(得分:0)

我建议您使用CI内置的* Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off Usage: flask run [OPTIONS] Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory. 助手,而不要为此创建定制库,

像这样在您的text中加载text助手:

autoload.php

在您看来,使用$autoload['helper'] = array('text'); character_limiter,无论您想要这样的内容:

word_limiter

更多信息:https://www.codeigniter.com/user_guide/helpers/text_helper.html#word_limiter