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.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *