user3459150
user3459150

Reputation: 35

how to limit numwords article in codeigniter?

i have cms blog with codeigniter. but when i limit content in article is not working. i am newbie in codeigntier can you tell me what am i to do.

this is cms_helper.php

    function get_excerpt($article, $numwords = 20){
    $string = '';
    $url = article_link($article);
    $string .= '<h2>' . anchor($url, e($article->judul_berita)) .  '</h2>';
    $string .= '<p class="pubdate">' . e($article->tanggal) . '</p>';
    $string .= '<p>' . e(limit_to_numwords(strip_tags($article->content), $numwords)) . '</p>';// content is not show
    $string .= '<p>' . anchor($url, 'Read more', array('judul_berita' => e($article->judul_berita))) . '</p>';
    return $string;
}

function limit_to_numwords($string, $numwords){
    $excerpt = explode(' ', $string, $numwords + 1);
    if (count($excerpt) >= $numwords) {
        array_pop($excerpt);
    }
    $excerpt = implode(' ', $excerpt);
    return $excerpt;
}

function e($string){
    return htmlentities($string);
}

in controller

    private function _homepage()
        {
            $this->load->model('mberita');
            $this->db->limit(6);
            $this->data['articles'] = $this->mberita->get_berita();

            //var_dump($this->data['articles']);
        }

please help me what to do. thank you.

Upvotes: 0

Views: 862

Answers (2)

Evgeny
Evgeny

Reputation: 102

You should use Codeigniter text helper:

$this->load->helper('text'); // add this to the the function or to the constructor

$string .= '<p>' . e(word_limiter(strip_tags($article->content), $numwords)) . '</p>';

In this article you can find many useful codeigniter text functions

Upvotes: 1

Karthik P
Karthik P

Reputation: 23

Try the below sample. this will surely work.

function limit_to_numwords($text,$no_words){
   $next=substr($text,$no_words,strlen($text));                        
   $spacepos=strpos($next," ");                     
   $desc=substr($text,0,$no_words+$spacepos)."...";
   return $desc;
}

From this, you can get exact full word text from the nearest space character. hope this will help your requirement.

Also, try to call the function as "$this->limit_to_numwords"

Upvotes: 0

Related Questions