我不知道是否有插件來啓用某種智能截斷。我需要用一個詞或一個句子的精確度來截斷我的文本。Rails:智能文本截斷
例如:
Post.my_message.smart_truncate(
"Once upon a time in a world far far away. And they found that many people
were sleeping better.", :sentences => 1)
# => Once upon a time in a world far far away.
或
Post.my_message.smart_truncate(
"Once upon a time in a world far far away. And they found that many people
were sleeping better.", :words => 12)
# => Once upon a time in a world far far away. And they ...
哦,那是一個很好的答案!非常感謝! 現在我看到一個真正的理由來增加我在正則表達式中的知識。 – gmile 2009-08-18 15:59:38
它不能像預期的那樣使用句子。它增加了句子之間的分隔符(點)。 – ciembor 2013-04-23 12:22:11
這樣做:'return s.split(/ \。(\ s | $)+ /)。reject {| s | s.strip.empty? } [0,opts [:sentences]]。map {| s | s.strip} .join('。')+'...'' – ciembor 2013-04-23 12:30:37