utilitees

- Addons for ExpressionEngine 1.7.x

TruncHTML

Version Released Type License
1.3.2 2009-09-02 Plugins Freeware (?)
⇒ TruncHTML for EE 2

TruncHTML Documentation
Docs

Download  TruncHTML
Download

Permalink TruncHTML
Permalink

TruncHTML Version RSS
Version RSS

This plugin is used for limiting text to a specified length. Much like the Character Limiter-Plugin that comes with ExpressionEngine.

The difference is, that TruncHTML is HTML-aware, so you can use it to shorten HTML-formatted text and keep the markup without being afraid of breaking it.

Unlike the standard limiter-plugins it does not count characters that belong to HTML-tags and does not cut-off in the middle of tags.


Example: Limit to 16 characters.
Take a look <a href="ref.html"><img src="arrow.gif" /></aat this page
TruncHTML:
Take a look <a href="ref.html"><img src="arrow.gif" /></aat 
Standard char_limit:
Take a look <a href="ref.html"><img&#8230; 


Furthermore it tries its best to close all tags that remain open after the text has been limited.

So if you limit the following text to 80 chars...
<class="quote"><b>Never let your sense of morals get in the way of doing whats right.<br />
<
em>Isaac Asimov</em></b></p

the result will be ...
<class="quote"><b>Never let your sense of morals get in the way of doing whats right.<br />
<
em>Isaac</em></b></p

instead of ...
<class="quote"><b>Never let your sense of morals get in the way of doing whats 

... which would probably screw the rest of the page.


[...]there wouldn’t be anything wrong with a separate plugin that is content aware. Silenz’s TruncHTML does a fabulous job with that, in fact I use it myself, and I don’t particularly see a reason for a first party solution to be made.[...]
Derek Jones, EllisLab

What does Freeware mean?
Freeware means you may use this addon free of
charge in as many installations as you like.


Comments

Jon:
2009-12-18 23:28

Just wanted to add a +1 for the feature of stripping out certain elements, or even stripping out certain elements with certain attributes (like a class name).  Like Sam, I too have images in my posts that I’d like to strip, but I also have captions that are made like so: caption here and it would be amazing to have the ability to target those as well.

Thanks again for the hard work on this plugin, it’s really worked out well for me!


Oliver:
2009-09-22 11:31

@Sam: Feature request noted for next update.


Sam:
2009-09-14 09:31

Hi Oliver,

I love the trunchtml pluin. I’m using it for an excerpt of a blog post. My problem is I have images in my post that I want stripped out.  Is there anyway that I can modify the plugin code to strip out certain html tags I set like an img tag?  Thank you for any help you can provide me.


Sean:
2009-05-15 03:32

Just a note to other users. If you want a link at the end to the full post and you put the link in the ending=”” parameter it will appear below the text. However if you add that link to the inline=”” parameter it will flow directly with the rest of the text.


Terjin:
2009-05-09 09:24

This plugin is great. Thank you so much. The only one that doesnt break the HTML compared to a host of others that I have tried!


Ben Seigel:
2009-03-03 05:41

Hi there,

Is it possible for you to add a dont_strip_tags feature to TruncHTML?  I’m working on a client’s site, and we’re applying TruncHTML to a field that has paragraph tags in it.  Since it closes the tag automatically, the “...” we want at the end of the excerpt shows up on a new line.

If you need some $ for this, I can re-bill it.

Please advise direct to bas AT expdesign DOTT net if you can.

Thanks,

Ben


Michael C.:
2009-02-12 20:33

And of course I couldn’t have remembered everything in the first post, now could I? hmmm

You’ll need to append this to the “With this” code segment in my previous post, to take into account cases where there aren’t any HTML tags in the parsed text:

// No HTML tags to put the inline string inside
          
if ($truncated == 0)
          
{
            $truncate 
.= $inline;
          

Michael C.:
2009-02-12 20:02

Hi Oliver,

I just spent an hour adding features to TruncHTML that I needed, only to discover that you had added most of them in the versions between 1.1.2 (which is on the EE site) and what’s here. wink

However, there is one thing I added to my customized version that is still missing from yours: placing the “inline” string outside of any inline tags, but inside of any block-level tags.

Currently, in your Asimov quote example, the “inline” string would be placed inside the still-open <em> and <b> tags. I’ve modified the plugin to place the “inline” string outside of all tags until the first closing block-level tag (p, div, or blockquote).

Here is the parts I modified for you to look at; if you want to include it in the next version, feel free to modify it as you see fit - I’m aware it’s a bit crude in its implementation. wink

Replace this:

if (!empty($inline)) {
                
if (substr($inline,0,1)=="_"{
                    $inline 
" ".ltrim($inline,"_");
                
}
              $truncate 
.= $inline;
          
}
          
          
foreach ($open_tags as $tag{
              $truncate 
.= '</' $tag '>';
          

With this:

// counter to track if the truncation string has been added to the output yet
    
$truncated 0
    foreach (
$open_tags as $tag)
    
{
        
if ((!empty($inline)) AND ($truncated == 0) AND ($tag == "p" OR $tag == "div" OR $tag == "blockquote"))
        
{
            
if (substr($inline,0,1)=="_")
            
{
                $inline 
" ".ltrim($inline,"_");
            
}
            $truncate 
.= $inline;
            
$truncated 1;
        
}
        $truncate 
.= '</' $tag '>';
    

Oliver:
2009-02-12 09:32

If you recently downloaded it from this site and actually installed it, it’s 1.3


Russ Lipton:
2009-02-11 18:24

I downloaded 1.3, but it appears to be 1.2.2?