这是我要做的:
require 'nokogiri'
doc = Nokogiri::HTML.parse <<-eot
<a href="/www.somethinggggg.com">Something 123</a>
eot
node = doc.at("a")
node.replace(node.text)
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>Something 123</body>
</html>
更新
如果我有一个包含链接内容的数组怎么办?
暗示
require 'nokogiri'
doc = Nokogiri::HTML.parse <<-eot
<a href="/www.foo.com">foo</a>
<a href="/www.bar.com">bar</a>
<a href="/www.baz.com">baz</a>
eot
arr = %w(foo bar baz)
nodes = doc.search("a")
nodes.each {|node| node.replace(node.content) if arr.include?(node.content) }
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>foo
bar
baz
</body>
</html>