Index of /dev/null

shorten by regex

While customizing my mt blog, I was wondering how to abbreviate long entry titles on particular places in a nice way. Well, mt provides template tag modifiers such as trim-to, but my aim was to do it more nicely, i.e. replacing anything behind the first three (or any other maximum) words by an ellipsis. For instance,

"more than three words"
should be shortened to
"more than three ..."
while a three-word-sentence should remain as it is.

The solution is a simple regex, of course. In Perl style,

s/ˆ(\S+(?:\s+\S+){2})(\s+\S+)+/$1 .../
does the job, because it doesn't match on three words or less. Equivalently, within mt template tags:
<$mt:EntryTitle regex_replace="/ˆ(\S+(?:\s+\S+){2})(\s+\S+)+/","$1&ensp;&hellip;"$>

no comments

post a comment

Leave a comment
You may use HTML tags for style