Avoiding words cut off in the middle at the end of php rss feed...

Hello friends,

I have the following php code which should display the first 100 characters of an external RSS feed on a specific place of my website. It works fine and I have even managed to add the three dots at the end of the description text, but I have no clue about what to do to avoid that the description is just cut off at character 100, if it is on the middle of a word. I would like that it goes back to the last blank space and cuts of the text at that place instead. Could you please tell me what lines I need to add to have this done? Please be specific to my file, as I am a real newbie and will probably not be able to figure out how to do it if you just give me a general indication. Thanks guys.

<?php $rss = new DOMDocument(); $rss->load('http://www.offshore-clan.com/blog/?feed=rss2'); $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 = 1; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $description = substr($description, 0, 100); $description = $description."..."; $date = date('l F d, Y', strtotime($feed[$x]['date'])); echo '

'; echo 'Posted on '.$date.'

'; echo '


'; } ?>

Hi there,

I’m afraid I haven’t tested this, but it should work:
$description = $feed[$x][‘desc’];
$description = substr($description, 0, 100);
$description = explode(" “,$description);
$description = implode(” “,$description).”…";

Sponsor our Newsletter | Privacy Policy | Terms of Service