Ruby 如何添加商标符号
我试图在HTML文档中的所有“想象游乐场”实例中添加一个商标符号。然而,我最终得到了这样的结果:Ruby 如何添加商标符号,ruby,nokogiri,Ruby,Nokogiri,我试图在HTML文档中的所有“想象游乐场”实例中添加一个商标符号。然而,我最终得到了这样的结果: <i class="fa fa-trademark"></i> 我使用的符号似乎已转换为HTML字符。我怎么能逃脱呢 这是我的原始Ruby代码: body = "<p>Whether you want to build a playground, make play a priority in your community, or
<i class="fa fa-trademark"></i>
我使用的符号似乎已转换为HTML字符。我怎么能逃脱呢
这是我的原始Ruby代码:
body = "<p>Whether you want to build a playground, make play a priority in your community, or learn more about Imagination Playground , we've got webinars for you in March!</p>
<p>As always, all our webinars are FREE. All you need to participate is a phone and a computer with an Internet connection.</p>"
new_body = Nokogiri::HTML(body)
new_body.encoding = 'UTF-8'
new_body.css('p','a').each{ |p|
p.content = p.content.gsub(/Imagination Playground\s/,'Imagination Playground<i class="fa fa-trademark"></i>');
puts new_body
body=“无论您是想建立一个游乐场,在您的社区中优先考虑游戏,还是想了解更多关于想象游乐场的信息,我们都会在三月份为您举办网络研讨会
一如既往,我们所有的网络研讨会都是免费的。您只需要一部电话和一台连接互联网的电脑即可参加。“
new_body=Nokogiri::HTML(body)
new_body.encoding='UTF-8'
新的_body.css('p','a')。每个{p|
p、 content=p.content.gsub(/Imagination playway\s/,'Imagination playway');
把新的身体
这就是我得到的:
<p>Whether you want to build a playground, make play a priority in your community, or learn more about Imagination Playground<i class="fa fa-trademark"></i>, we've got webinars for you in March!</p>
<p>As always, all our webinars are FREE. All you need to participate is a phone and a computer with an Internet connection.</p>
无论您是想建造一个游乐场,让游戏成为社区的优先事项,还是想更多地了解想象游乐场i class=“fa fa trandmark”/i,我们都会在三月份为您举办网络研讨会
和往常一样,我们所有的网络研讨会都是免费的。你只需要一部电话和一台连接互联网的电脑就可以参加
如何替换HTML段落并转义符号和特殊字符?以下是我的做法:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<p>Whether you want to build a playground, make play a priority in your community, or learn more about Imagination Playground , we've got webinars for you in March!</p>
<p>As always, all our webinars are FREE. All you need to participate is a phone and a computer with an Internet connection.</p>
EOT
doc.encoding = 'UTF-8'
doc.css('p').each do |p|
p.children = p.content.gsub(/Imagination Playground\s/, 'Imagination Playground<i class="fa fa-trademark"></i>')
end
puts doc
开始时看起来像这样:
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body><p>Imagination Playground<b>foo</b></p></body></html>
非常好的解释!!如果我想保留段落中的html标记,请尝试对某些测试执行此操作,然后使用粗体
require 'nokogiri'
doc = Nokogiri::HTML('<p>Imagination Playground<b>foo</b></p>')
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body><p>Imagination Playground<b>foo</b></p></body></html>
doc.search('p').each do |p|
p.children = p.children.to_html.gsub(/Imagination Playground\s?/, 'Imagination Playground<i class="fa fa-trademark"></i>')
end
puts doc
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body><p>Imagination Playground<i class="fa fa-trademark"></i><b>foo</b></p></body></html>
doc.search('p').first
# => #(Element:0x3fd222462204 {
# name = "p",
# children = [
# #(Text "Imagination Playground"),
# #(Element:0x3fd2224608f0 { name = "b", children = [ #(Text "foo")] })]
# })
doc.search('p').first.children
# => [#<Nokogiri::XML::Text:0x3fd222461688 "Imagination Playground">, #<Nokogiri::XML::Element:0x3fd2224608f0 name="b" children=[#<Nokogiri::XML::Text:0x3fd22245fe64 "foo">]>]