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