Rails中是否有HTML安全截断方法? [英] Is there an HTML safe truncate method in Rails?

查看:93
本文介绍了Rails中是否有HTML安全截断方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails中有一串HTML。我想在包含HTML标记的特定字符数之后截断字符串。另外,如果拆分恰好落在开始和结束标签的中间,我想关闭打开的标签。例如;

I have a string of HTML in Rails. I'd like to truncate the string after a certain number of characters not including the HTML markup. Also, if the split happens to fall in the middle of an opening and closing tag, I'd like to close the open tag/s. For example;

html = "123<a href='#'>456</a>7890"
truncate_markup(html, :length => 5) --> "123<a href='#'>45</a>"


推荐答案

有两种完全不同的解决方案, : truncate_html

There are two completely different solutions both with the same name: truncate_html


  1. https://github.com/ianwhite/truncate_html :这是一个gem并使用了一个html解析器(nokogiri)
  2. href =https://github.com/hgmnz/truncate_html =noreferrer> https://github.com/hgmnz/truncate_html :这是一个你放在你的助手目录中的文件。它使用正则表达式并且没有依赖关系。

  1. https://github.com/ianwhite/truncate_html : This is a gem and uses an html parser (nokogiri)
  2. https://github.com/hgmnz/truncate_html : This is a file you put in your helpers directory. It uses regular expressions and has no dependencies.

这篇关于Rails中是否有HTML安全截断方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆