duanlu1908 2014-04-10 02:11
浏览 32

如何限制codeigniter中的numwords文章?

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.

  • 写回答

2条回答 默认 最新

  • dsgk0386 2014-04-10 06:42
    关注

    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"

    评论

报告相同问题?

悬赏问题

  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件