Jump to content

[SOLVED] Trimming with strlen question..


acer0169

Recommended Posts

I'm sure this is an easy thing to do, but I can't seem to get it working..

 

I've been using this..

 

substr($row['title'], 0, 55)

 

..to cut my strings down to 55 characters, but this often cuts a word in half. I would like to know how I can cut off a string after the last whole word which fits within a 55 character limit.

 

Thanks in advance,

Ace.

 

(first post also, hi everyone!!) ;D

Link to comment
https://forums.phpfreaks.com/topic/168262-solved-trimming-with-strlen-question/
Share on other sites

Unless there's already a function for this, probably the easiest way would be to use the wordwrap function to get lines of the desired lenth, then explode the string and use the first index of the resulting array.

 

Alternatively, you could explode the string by the space, and loop through the array checking the length of each part. If adding the word doesn't take you over the limit, append it to the generated string. If it does, don't add it and break out the loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.