PHP说明摘录 [英] PHP Description Excerpt

查看:48
本文介绍了PHP说明摘录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,需要它仅在描述中回显100个单词或更少,而不是整个描述。

I have the following code and need it to only echo out 100 words or less in the description rather than the entire description. Is there anyway to do that by editing this code?

public static function getExcerpt($profile) {
    $out='';
    if(!empty($profile['description'])) {
        $out.=$profile['description'].' '.__('', 'lovestory');
    }

    return $out;
}

谢谢!

推荐答案

// for 100 characters...
if (strlen($profile['description']) > 100)
    $description = substr($profile['description'], 0, 100) . "...";
else
    $description = $profile['description'];

$out.= $description . ' ' . __('', 'lovestory');


// for 100 words
$out.= implode(" ", array_slice(explode(" ", $profile['description']), 0, 100)) .' '.__('', 'lovestory');

这篇关于PHP说明摘录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆