To make decent excerpts, I needed something that stripped out all tags first and then limited the remaining words, all in one fell swoop, so I combined those functions into one plugin. Rick Ellis originally wrote Word Limit, then Vik Rubenfeld modified that into Word Limit Plus. I took Vik’s modification and added some HTML stripping for your eexcerpting pleasure.
For some background on this plugin, you can review these threads:
if_exceeds - Text will be truncated if it is greater than this number of words. This parameter must be included.
stop_after - Text greater than the number of words contained in the if_exceeds parameter, will be truncated to the word length stored in the stop_after parameter. This parameter must be included. Must be less than the number of words in the if_exceeds parameter.
the_link - Optional. A link back to the original article.
Great. Thank you! You can see it in action in the left “News” column at branchenwissen.info.
Mdesign, do you see any chance to add an parameter/function to handle line breaks/paragraph so that they are more or less preserved? On a fast glance, it seems the preg_replace() uses a clever regex to filter out all tags, so one would perhaps replace br and opening p tags with some marker, filter all remaining tags and then replace the marker with br / tags? But I don’t want to mess with your code, I just would like to see an option to have the layout of the excerpt somewhat preserved but tags stripped to guarantee consistent HTML coding.
Great. Thank you! You can see it in action in the left “News” column at branchenwissen.info.
Mdesign, do you see any chance to add an parameter/function to handle line breaks/paragraph so that they are more or less preserved?
Ausgezeichnet! Gesundheit!
I didn’t want to open a big can of worms with this plugin by introducing parameters. I had a pretty specific need - which I find repeating iteself on numerous sites I work on - which is real basic excerpts with no tags in them. I really only intended this to be used with a sentence, or maybe a few sentences. Heck, all I did was take an existing plugin and throw some HTML stripping in there first. I think it would be pretty easy to keep line breaks intact, though.
Hmm, you might be on to something there though. First go through and replace <p> with [p] (or something), then go through and wipe out tags, then replace [p] with <br />. Is that what you were thinking?
Yes, something like this - just that I would perhaps use some either low or high ascii character as the “marker”/replacement for <p>, <br> and <br /> that most likely in the text processed will not be used (perhaps bell, 0x07, or something like that). Reason is this would count one character, whereas [p] would count three (so, [p] could perhaps truncated to [p or [ afterwards). Then use the preg_replace, truncating and so on as your plugin already does - and in a last step, replace the marker with <br /> or <br /><br />.
Of course, it would need a parameter because for most cases the current usage is great, just to truncate longer news the line/paragraph breaks would be nice to have.
Markus - I just came upon the need to preserve line breaks myself. I think I’ll look at updating this plugin to preserve those if possible - when I get a spare moment .
great mod to the plugin. i like to ask for a feature to be considered: the ability to choose between “character unit” vs. “word unit”. sometimes, i need to truncate the post by the number of characters, regardless of the number of words.
Looks like a very nice upgrade! I’ll try it out. I second the request for choosing characters or words. Using characters gives you a little more control over the space the excerpt will fill. (Some posts use more big words than others.) Why not eliminate the need for char_limit too, eh?
Hmm, I thought segment variables were parsed very early on, so I’m assuming that link should work. When you say “totally ignored”, what does that mean, exactly? It returned nothing?
I’m showing weblog entries - and if an entry is shorter than X I want the whole thing to show. If it is longer than X, I want to show X words, and then have a “show entire post” link, but instead of that link taking someone to a new page, I want to use javascript and “getElementById” to show the entire post on that same page without a reload.
Any thoughts on how I could get this extension to do that? Or should I use this as a jumping point for a new extension?
Sorry everyone, somehow I wasn’t getting messages from this thread. When I get a little time, I’ll re-read the last few posts and see what I can do (right in the middle of a project at the moment…)
great plugin but i get an error
Notice: Undefined offset: 70 in /plugins/pi.eexcerpt.php on line 80
any ideas? help?
I’m getting the same error. Well, similar in that the offset number is different.
Notice: Undefined offset: 16 in /plugins/pi.eexcerpt.php on line 80
I think the problem is when there are extra spaces at the end of the line. For example, I have my short description line to cut off after 16 words. My content manager put in a 14 word phrase, but during the copy and paste, he ended up putting 2 or 3 extra spaces at the end of the line. This caused the error. Once I removed the spaces, it works fine.
Is there some extra code you can add that will truncate any extra white space at the end of the line?