How can i truncate a text to the closest position with rails 3 whithout cut in the middle of a word?
如何将文本截断到最靠近轨道3的位置,而不是在单词的中间切割?
For exemple, I have the string :
例如,我有字符串:
"Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum."
If i cut it, i want to cut like this :
如果我切它,我想像这样切:
"Praesent commodo cursus magna, vel scelerisque nisl ..."
And not :
并不是 :
"Praesent commodo cursus magna, vel scelerisque nisl conse..."
3 个解决方案
#1
44
If you pass in a separator to the truncate method it will perform a natural word break instead of truncating at a middle of a word
如果将分隔符传递给truncate方法,它将执行自然的单词分隔,而不是在单词的中间截断
Something like this should work (vary the length to whatever you want to remove it altogether if you want the default of 30 characters):
这样的东西应该工作(如果你想要30个字符的默认值,可以将长度改为你要删除它的任何长度):
truncate("Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.", :length => 17, :separator => ' ')
More information about the options you can have in truncate can be found in the Documentation
有关截断中可以使用的选项的更多信息,请参阅文档
#2
6
Truncate is a great option, but if you want to have complete word detection, regex is your solution. I would recommend something like this:
截断是一个很好的选择,但如果你想要完整的单词检测,正则表达式是你的解决方案。我会推荐这样的东西:
string.match(/^.{0,30}\b/)[0]
Or you can put this in a function
或者你可以将它放在一个函数中
def shorten(string, count)
string.match(/^.{0,#{count}}\b/)[0]
end
Update
According to Rails documentation, you can pass regex into the truncate method, like so:
根据Rails文档,您可以将正则表达式传递给truncate方法,如下所示:
'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
Both of these options offer far better word boundary detection than passing in a space character into the truncate method.
这两个选项都提供了比将空格字符传入truncate方法更好的字边界检测。
#3
5
Starting Rails 4.2
there is a new ActiveSupport
method called string#truncate_words
. It truncates a string by number of words which makes it impossible to have a cut in the middle of a word.
启动Rails 4.2有一个名为string#truncate_words的新ActiveSupport方法。它按字数截断字符串,这使得无法在单词的中间进行剪切。
'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)')
which returns
返回
"And they found that many... (continued)"
#1
44
If you pass in a separator to the truncate method it will perform a natural word break instead of truncating at a middle of a word
如果将分隔符传递给truncate方法,它将执行自然的单词分隔,而不是在单词的中间截断
Something like this should work (vary the length to whatever you want to remove it altogether if you want the default of 30 characters):
这样的东西应该工作(如果你想要30个字符的默认值,可以将长度改为你要删除它的任何长度):
truncate("Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.", :length => 17, :separator => ' ')
More information about the options you can have in truncate can be found in the Documentation
有关截断中可以使用的选项的更多信息,请参阅文档
#2
6
Truncate is a great option, but if you want to have complete word detection, regex is your solution. I would recommend something like this:
截断是一个很好的选择,但如果你想要完整的单词检测,正则表达式是你的解决方案。我会推荐这样的东西:
string.match(/^.{0,30}\b/)[0]
Or you can put this in a function
或者你可以将它放在一个函数中
def shorten(string, count)
string.match(/^.{0,#{count}}\b/)[0]
end
Update
According to Rails documentation, you can pass regex into the truncate method, like so:
根据Rails文档,您可以将正则表达式传递给truncate方法,如下所示:
'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
Both of these options offer far better word boundary detection than passing in a space character into the truncate method.
这两个选项都提供了比将空格字符传入truncate方法更好的字边界检测。
#3
5
Starting Rails 4.2
there is a new ActiveSupport
method called string#truncate_words
. It truncates a string by number of words which makes it impossible to have a cut in the middle of a word.
启动Rails 4.2有一个名为string#truncate_words的新ActiveSupport方法。它按字数截断字符串,这使得无法在单词的中间进行剪切。
'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)')
which returns
返回
"And they found that many... (continued)"