Forum Moderators: coopster

Message Too Old, No Replies

Adding dots to too long titles

         

Psycho Mantis

11:58 am on Jan 10, 2005 (gmt 0)

10+ Year Member



I'd like to add '...' to titles of calendar entries, which are too long.

The sting for the title is 100 characters long.

I figured I could search for the end of the string with str_word_count and replace the last three characters, but somehow I couldn't make it work.

Could someone please tell me how to do this?

Sorry for such a bold request, but I only have very basic understanding of PHP/MySQL.

Salsa

12:29 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



This is untested, but I think it should work for you.

if (strlen($title > 100)) { 
$title = substr($title,0,100); // cuts title off at 100 chars
$title = substr($title,0,strrpos($title,' ')-1)."...";
// ^ cuts off last space and possible partial word and adds the ...
}

I hope this helps.

Nutter

1:14 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



This is a slower way, but it makes it end on a full word instead of in the middle of one.

Split the string into an array with explode(" ", $string). Step through the array until the total characters are too long and then step back one. Implode back the right number of words and append the ellipse.

- Ryan

Salsa

2:28 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



Hi Ryan,

My suggestion was meant to exclude partial words at the end of the string. If you tested it and it didn't work, it probably doesn't need the -1 in the strrpos function. I can never remember where those strpos functions begin and end until I try them.

I wish you well.

Nutter

6:24 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



You're right, I misread the question. Mine is from a page where I needed the first n words, not the first n characters.