limit amount of data displayed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

limit amount of data displayed

Post by ianhull »

hi guys,

another one for you,

i have a details field and i would like to retrieve and only display the first 150 characters then show a "..."

how is this done? php5 mysql5

you see pages that start a sentence and then show ...more>> etc.

please help

thanks
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Code: Select all

substr()
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

You can try working with :

Code: Select all

$file_length = strlen($file);
						
if($file_length > 150)
{
  $file = substr($file, 0, 150);
  $file = $file."...";	
}
... where after 150 characters it will display the elipse.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

I use this to prevent truncate words and breaking tags

Code: Select all

function reduceText($string)
{
$string = strip_tags($string);

	//Search the end of a word after [, int offset] and set the result as limit
	$limit = @strpos($string, " ",150);
	if($limit){
		//Use $limit to replace width ...Text
		return substr_replace($string, "...",$limit);
	}
	else{
		return $string;
	}
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Check Useful Posts too. ;)
Post Reply