Php 如何限制codeigniter中的numwords文章?

Php 如何限制codeigniter中的numwords文章?,php,codeigniter,Php,Codeigniter,我有cms博客与codeigniter。但当我限制文章的内容时,这是行不通的。我是Codeignier的新手,你能告诉我该怎么做吗 这是cms_helper.php function get_excerpt($article, $numwords = 20){ $string = ''; $url = article_link($article); $string .= '<h2>' . anchor($url, e($article->judu

我有cms博客与codeigniter。但当我限制文章的内容时,这是行不通的。我是Codeignier的新手,你能告诉我该怎么做吗

这是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);
}

请帮我做什么。谢谢。

试试下面的示例。这肯定会奏效

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;
}
由此,您可以从最近的空格字符中获得精确的全文。希望这将有助于您的要求


另外,尝试将函数调用为“$this->limit_to_numwords”

您应该使用Codeigniter文本助手:

$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>';
$this->load->helper('text');//将其添加到函数或构造函数中
$string.=''。e(单词限制器(带标签($article->content),$numwords))。'

",;
在本文中,您可以找到许多有用的codeigniter

$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>';