Say for example I have the following code:
<h3>My very long title</h3>
<h3>Another long title</h3>
If I wanted to shorten these titles using PHP or jQuery, how can I trim them to the nearest word and append an ellipsis? Is it possible to specify a character count?
<h3>My very long...</h3>
<h3>Another long...</h3>
Edit - How can I do this for each one of the headlines? I don't really have an idea as to how to pass each headline into a string...
Thanks
See this question:
Edit: (including <= $limit check)
This is easy using a PHP function. Look at this example:
You can then pass in text with a function like:
(I haven't tested this, but it should work perfectly.)
If you want to be within a specific length, this will work.
Tested, works perfectly.
Edit: turned into function.
Creating an ellipsis in PHPThis function can then be used in the following way:
jquery Auto Ellipsisusing some php and the mighty regular expressions
The same method should be possible in javascript using regex and jquery.
Jquery also have the ellipsis plugin, you might want to look into it.
Maybe you're searching for wordwrap().
Use $break to break the line using the optional break parameter. If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart.
Check out function documentation on php's site for more examples.
+++
Another solution would be to split title by
' '
(a space) using explode() and provide a limit to say max 5 words, than cut off the last element of array using array_pop and finally joining them with implode() using' '
(that space) as glue. But this solution is not the best as it will give you ugly output if you have long words.