neat_trim() – Handy for tailored drupal teasers

In drupal there are many ways to go about putting together teasers for custom content types created with the Content Creation Kit (CCK). The quickest to get up and running with is often to use the Content Templates (Contemplate) module, this allows you to specify in-line markup and PHP with code to get at and display the node’s data fields – formatted just as you require.


Often when putting together a teaser you may want to neatly truncate some long text to a summary and append ‘…’ or the like to the end – well there’s a great little PHP function which does just this called neat_trim() it is provided by Justin Cook and does the job  nicely!


One thing to note however is that HTML mark-up as well as newlines and carriage-returns in the input string can confuse this function.  To get around this problem you can arrange to remove the offending material either by modifying the function or by stripping it out of the input string before calling the function.  Use can use strip_tags() to remove HTML mark-up.