ruby-on-rails - 如何在ruby中将最后一个可用空间替换为nbsp
问题描述
如何将最后一个可用空间替换
为ruby ?
我在数据库中有这个:
<h1>Hello dear friend!</h1>
<p>How are you?</p>
<figure><img src="..." alt="..." /></figure>
<p>Bye!</p>
我需要有这个“输出”:
<h1>Hello dear friend!</h1>
<p>How are you?</p>
<figure><img src="..." alt="..." /></figure>
<p>Bye!</p>
我试着和 nokogiri 一起玩:
text = Nokogiri::HTML::DocumentFragment.parse(...)
text.css('h1, h2, h3, h4, h5, h6, p, li').each do |tag|
tag_arr = tag.content.split(' ')
tag_last_words = tag_arr[tag_arr.length-2..tag_arr.length]
tag_return = tag_arr[0..-2].push(tag_last_words.join(' '))
tag_return = tag_return.join(' ')
tag.content = tag_return
end
但我无法击败一些“错误”:
- 所有属性和内部标签 (html) 都被删除
- 而不是
我有&nbsp;
为什么?避免在移动设备上换行换行。(在我的情况下,JS 不是一个选项)
解决方案
推荐阅读
- terminal - 当我运行新终端时显示错误
- sql - SQL replace cte by actual table to improve performance?
- azure - 东亚和东南亚的数据中心位于哪个国家
- python - Django Import Error. Cannot import name reciever
- python-3.x - How to set a filter as default filter in odoo
- angular - 使用 ngx-infinite-scroll 进行分页和搜索
- sql - 空间查询(在多边形中查找点)不应该返回任何内容
- linux - 如何在文件写入时防止其他人读取/写入文件
- c++ - boost::interprocess, get_free_memory()
- deep-learning - 预训练神经网络在图像中执行二值分割的有用性