0

I usage this php code to display recent post with description. Max desc 100 word. How to display 3 dot after 100 word (like this: this is content of bla bla bla... ).

Can someone help me, please!

This is my php code:

<?php
$rss = new DOMDocument();
$rss->load('http://domain.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substr($description, 0, 138); 
$date = date('d F Y - H:m', strtotime($feed[$x]['date']));
echo '<li><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong>';
echo '<br><small>'.$date.'</small>';
echo '<br>'.$description.'</br></li>';
}
?>
4

1 回答 1

0

如果我像这样查看您的代码,但这是在计算每个符号,而不是单词:

if(strlen($description) > 138) {
    $description = substr($description, 0, 138).'&hellip;'; 
}

如果您想使用单词,请执行以下操作:

// put this function above your code....
function string_limit_words($string, $word_limit=100, $ending = '&hellip;') { 
    $words = explode(' ', $string); 
    if(count($words) > $word_limit) {
        return implode(' ', array_slice($words, 0, $word_limit)).$ending; 
    }
    return $string;
}

// and use it like this...
echo '<br>'.string_limit_words($description).'</br></li>';
于 2013-02-16T14:00:14.310 回答