CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: Adding "..." at the end of a text string  (Read 3412 times) Bookmark and Share
Bill220
Newbie
*
Posts: 4



View Profile
« on: Dec 08, 2010, 11:57:42 am »

I'd like to be able to automatically truncate titles if they're too long in a guestbook, and I'd like to automatically add "..." and a "read more" link at the end. Does anyone have any examples of doing this?
Logged
Mr. Crane
Jr. Member
*****
Posts: 63



View Profile WWW
« Reply #1 on: Dec 08, 2010, 12:53:37 pm »

Easy way to do it in a single line:

Code:  
Highlight Mode: (PHP)
  1. if(strlen($text) > 50){$text = substr($text, 0, 45) . '...';
 

If the length of $text is more than 50 characters it's truncated to 45 characters and '...' is added on the end. You could add a "Read more" link like this:

Code:  
Highlight Mode: (PHP)
  1. if(strlen($text) > 50){$text = substr($text, 0, 45) . '...'; <a href="somepage.htm">Read more</a>
 
« Last Edit: Dec 08, 2010, 12:57:17 pm by Mr. Crane » Logged

Traveling through the Digital Age at 33⅓rd revolutions per minute.
Tags:
Pages: 1
  Print  
 
Jump to: