只返回指定数量的单词


return only specified number of words

我有一些HTML标记的文本,我只想输出其中的40个单词。

例如

<strong>This is an article </strong> containing 150 words with <a href="">HTML
</a>tags and I want to output only first 40 words. How to do this?

我现在正在使用nl2br,因为它有EOL。CCD_ 1和CCD_。

所以经过一些谷歌搜索,我找到了我想要的东西(在这个论坛上http://www.webmasterworld.com/forum88/10821.htm)

该函数从字符串中剪切指定数量的字符,然后将字符添加到下一个空格(以防止在单词中间剪切)。

function elliStr($string,$noChars) { 
    for ($i = 0; $i < strlen($string); $i++) { 
    $result = ($noChars+$i >= strlen($string) ? $string : ($string{$noChars+$i} == " " ? substr($string,0,$noChars+$i) : ""));
    if ( $result != "" ) {
        return nl2br($result);
        } 
    }
}