word count returns result only for half of my query results

czechmate1976
czechmate1976 used Ask the Experts™
on
I have a script to select 5 latest articles and display its title, thumbnail image and 20 words of each  article content in a news column on the homepage, however only 2 of the query results display those 20 words of the article content. Either the first 3 or the last three do not display those 20 words of the article content depending if I ORDER BY date ASC (or DESC). Its very weird and I can't find what is the cause of the behaviour. I am using a function to shorten the article to the required 20 words (function 1 - below) and a code on the homepage to go through the query results and display those 5 shortened articles. Can anyone please help me to find the mystery? Thanks a lot to everyone.
function shorten_string($string, $wordsreturned) {
	//returns the first required number of words ($wordsreturned)
	//if string has less words than $wordsreturned the entire string is returned
	$retval = $string;
	
	$array = explode(" ", $string);
	if(count($array)<= $wordsreturned) { // short enough already
			$retval = $string;
	} else { // removing words over $wordsreturned
			array_splice($array, $wordsreturned);
			//echo $wordsreturned." ";
			$retval = implode(" ", $array)."...";
	}
	return $retval;
}
 
//code from the homepage
<?php
						$query = "SELECT * FROM tblnews ORDER BY created DESC LIMIT 5";
						$news_set = mysql_query($query, $connection);
						confirm_query($result);
						if(mysql_num_rows($news_set)>=1) {
							while ($article = mysql_fetch_array($news_set)) {
								echo "<div class=\"news-feed-column-box\">";
								echo "<a href=\"news.php?nid=".$article['nid']."\"><h4 class=\"news-feed-title\">".$article['newstitle']."</h4></a>";
								if(!empty($article['image_name'])) {
									$image = "../image_dir/".$article['image_name'];
									echo "<img src=\"includes/show_thumbnail.php?filename=".$image."&amp;width=45&height=30\" class=\"news-img\" />";
								}
								echo "<p class=\"news-feed\">".shorten_string($article['content'], 20)."</p>";
								echo "</div>";
							}
						}
					?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
perhaps you have multiple spaces ...

$array = explode(" ", ereg_replace(' +',' ',$string));

Commented:
I afraid that some of your articles beak the HTML. Please generate HTML source and look at.
Commented:
or remove the tags :

$array = explode(" ", ereg_replace(' +',' ',strip_tags($string)));

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Commented:
For safe shortening replace:
$array = explode(" ", $string);
with
$array = explode(" ", strip_tags($string));

Author

Commented:
The thing is why does it work for two out of five and not for all of them when its basically looping through .. if there was something totally wrong.. none of them would display, no? But I may be wrong.. I will still try some of these tips and very grateful, guys, thanks

Author

Commented:
flob9, thanks a lot!!! You hit the nail! Now, can you please explain to me why my simple statement worked only for 2 and this works for all of them. What exactly does it do? I am still a student and learning. It really helps if PRO's explain these things in plain words sometimes..

Commented:
The ereg_replace removes duplicates spaces. The strip_tags remove html tags from the string.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial