views:

62

answers:

3

Hi, I'm looking for a way to limit a string in php and add on ... at the end if the string was too long.

Thanks :)

+1  A: 

You might want to take a look at this answer if you don't want to split words.

If you don't care about splitting words, then simply use the php substr function.

Sev
+2  A: 

Do a little homework with the php online manual's string functions. You'll want to use strlen in a comparison setting, substr to cut it if you need to, and the concatenation operator with "..." or "…"

dlamblin
+1  A: 
if (strlen($str) > 10)
   $str = substr($str, 0, 7) . '...';
bramp
I guess you didn't think he might be skipping his homework reading assignments.
dlamblin
Sorry no I didn't consider that. What are the rules for homework?Oh and wait, I just noticed, my code doesn't even work correctly ;)
bramp
It's just a community rule: http://meta.stackoverflow.com/questions/10811/how-to-ask-and-answer-homework-questions
dlamblin
nah I wasn't skipping homework I'm only in grade 10 and all we do is vb.net which is crap. Btw i'm not that dumb :P I can fix the rest of it, I just didn't know how I should go about doing it.
Belgin Fish