duanguilin2007 2012-01-24 16:05
浏览 17

显示帖子摘录,受字数限制

I am working on my php website (Not a Wordpress site) on the main index I display the two newest post. The thing is on the description it shows the entire article I find myself needing to display post excerpts maybe 35 word limit.

<?=$line["m_description"]?>

<?
$qresult3 = mysql_query("SELECT * FROM t_users WHERE u_id=".$line["m_userid"]." LIMIT 1");
if (mysql_num_rows($qresult3)<1) { ?>
  • 写回答

2条回答 默认 最新

  • douzhong1730 2012-01-24 16:10
    关注

    I have a function though other people may say it's not good because I'm still good at PHP too (tips welcome people) but this will give you what you are looking for, it may need better coding if anyone has suggestions.

    function Short($text, $length, $url, $more){
    $short = mb_substr($text, 0, $length);
    
    if($short != $text) {
        $lastspace = strrpos($short, ' ');
        $short = substr($short , 0, $lastspace);
    
        if(!$more){
            $more = "Read Full Post";
        } // end if more is blank
    
        $short .= "...[<a href='$url'>$more</a>]";
    } // end if content != short
    
    $short = str_replace("’","'", $short);
    $short = stripslashes($short);
    $short = nl2br($short);
    
    } // end short function
    

    To Use:

    say your article content is the variable $content

    function($content, "35", "http://domain.com/article_post", "Read Full Story");
    echo $short;
    

    Similarly, you can adjust the function to remove $url and $more from it and just have the excerpt with ... at the end.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号