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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.