0

このphpコードを使用して、最近の投稿と説明を表示します。最大desc100ワード。100語の後に3ドットを表示する方法(このように:これはbla bla bla ..の内容です)。

誰か助けてくれませんか!

これは私のphpコードです:

<?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 に答える