We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Eexcerpt - Strip HTML and limit words

Development and Programming

Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

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:

Autogenerating an excerpt when a summary doesn’t exist Definitive answer on Filter_HTML status, update or alternative, please

USAGE: Wrap anything you want to be processed between the tag pairs. Works exactly like word_limit_plus, but strips tags.

{exp:eexcerpt if_exceeds="600" stop_after="500" 
the_link="<a href='{title_permalink=news/item}'>MORE...</a>"}

text you want processed

{/exp:eexcerpt}

REFERENCE:

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.

The download is now over at its designated page at masugadesign.com: http://www.masugadesign.com/the-lab/scripts/eexcerpt/

       
ms's avatar
ms
274 posts
17 years ago
ms's avatar ms

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.

-Markus

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.
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 with [p] (or something), then go through and wipe out tags, then replace [p] with . Is that what you were thinking?

       
ms's avatar
ms
274 posts
17 years ago
ms's avatar ms

Yes, something like this - just that I would perhaps use some either low or high ascii character as the “marker”/replacement for , and 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 or

.

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

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

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

As you can see, we were having a discussion about that over on this thread.

       
P.J.'s avatar
P.J.
158 posts
17 years ago
P.J.'s avatar P.J.

mdesign,

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.

thanks for your consideration.

       
oldgoldblack's avatar
oldgoldblack
113 posts
17 years ago
oldgoldblack's avatar oldgoldblack

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?

Thanks for the work and thanks for sharing.

       
bjsteiger's avatar
bjsteiger
87 posts
17 years ago
bjsteiger's avatar bjsteiger

Masuga - is it possible to set the link manually in:

{exp:eexcerpt if_exceeds="600" stop_after="500"
the_link="<a href='{title_permalink=news/item}'>MORE...</a>"}

I tried to change it to read:

{exp:eexcerpt if_exceeds="600" stop_after="500"
the_link="<a href='/{segment_1}/{segment_2}/{segment_3}/category/{url_title}'>MORE...</a>"}

When I did this, it totally ignored the URL.

bjs

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

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?

       
bjsteiger's avatar
bjsteiger
87 posts
17 years ago
bjsteiger's avatar bjsteiger

Yes…basically the link wouldn’t even appear, but the text wold be truncated as expected

bjs

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

Could this be used for the following situation?

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?

Thanks!

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

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…)

       
alterracoffee's avatar
alterracoffee
8 posts
about 17 years ago
alterracoffee's avatar alterracoffee

great plugin but i get an error Notice: Undefined offset: 70 in /plugins/pi.eexcerpt.php on line 80 any ideas? help?

       
goodbytes's avatar
goodbytes
49 posts
16 years ago
goodbytes's avatar goodbytes

Thanks for this plugin. I’m using it to automatically generate my <description> tag with some 30 words. Great work!

       
Ryan M.'s avatar
Ryan M.
1,511 posts
16 years ago
Ryan M.'s avatar Ryan M.

@alterracoffee: I didn’t see your post. Are you still getting this error?

@goodbytes: Glad you’re finding it useful!

       
1 2 3 Last

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.