Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cut off paragraph after a certain number of characters

Status
Not open for further replies.

dontpunchme

Programmer
Jul 29, 2002
20
US
Hi,

I have a field that is displayed on my website. It currently has a few hundred characters in it.

Is there a script I can wrap around it, to limit the number of characters to show the first 150? Also.. is there a way to not have it cut off in the middle of a word?

Sample:
(Before)
Peja Stojakovic scores 16 of his 20 points in the second half to lead the Kings to a 94-92 win over Dallas, putting Sacramento one win away from advancing to the next round.


(After)
Peja Stojakovic scores 16 of his 20 points in the second half to lead the Kings to a 94-92 win over Dallas, putting Sacramento one win away...

Thanks for your help.

Mike
 
Ahh... Mavs lost. This should work:
Code:
<script language="javascript">
function cutoff( str, len )
{ 	ret = str;
	if (ret.length > len)
	{	ret = str.substring( 0, len+1 );
		ret = ret.substring( 0, ret.lastIndexOf(" ")) + "...";
	}
	return( ret );
}
</script>
 
Thanks for the info.

Where would my text actually go into this script... (or the database call to pull my text in)?

Thanks.
 
Database call... are you using server-side language (a la PHP/ASP)? If true, it is probably better to make cutoff there. Javascript is pretty much limited to client-side (browser).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top