beta BLOG dot NET

/* bugs, features, drafts, and solutions. */

// shorten by regex

admin1 blogged on 2009-10-15T21:35:50+00:00

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,

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

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

[-] hide code
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:
[-] hide code
<$mt:EntryTitle regex_replace="/ˆ(\S+(?:\s+\S+){2})(\s+\S+)+/","$1&ensp;&hellip;"$>


# 

$tags

 = [  ];
# 

$categories

 = [  ];
# 

no comments

, 

no trackbacks

→ leave a comment


# 

trackback_url

( http://mt42.beta-blog.net/script/trackback/194083 );
here goes the message.