Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 用附加信息替换链接,如何排除<;img src=”等;http://quot;标签_Ruby On Rails_Ruby_Regex - Fatal编程技术网

Ruby on rails 用附加信息替换链接,如何排除<;img src=”等;http://quot;标签

Ruby on rails 用附加信息替换链接,如何排除<;img src=”等;http://quot;标签,ruby-on-rails,ruby,regex,Ruby On Rails,Ruby,Regex,我正在处理论坛帖子,我需要用特殊格式(包括图标等)替换用户提交的url。用户输入可以是以下内容: <p>This is a link: http://www.url1.com/</p> <p>http://www.url2.com/</p> <p><img src="http://www.url3.com/image.jpg"> something</p> 这是一个链接:http://www.url1.com/

我正在处理论坛帖子,我需要用特殊格式(包括图标等)替换用户提交的url。用户输入可以是以下内容:

<p>This is a link: http://www.url1.com/</p>
<p>http://www.url2.com/</p>
<p><img src="http://www.url3.com/image.jpg"> something</p>
这是一个链接:http://www.url1.com/

http://www.url2.com/

某物

具体来说,我需要用修改过的数据(需要在一个代码块内修改约30行代码)替换前两个链接(url1.com和url2.com)。然而,我还没有找到一个好的方法来实现这样的事情

如果我这样做

html.gsub(/http[s]?:\/\/[^(\s|<)]+/) { |url| "REPLACED" }

html.gsub(/http[s]?:\/\/[^(\s|您可能希望避免使用正则表达式进行html/XML攻击。请尝试使用nokogiri。请参阅

[更新]

[更新]

我已经对上述问题进行了重新处理,现在就是了

require 'cgi'
require 'rubygems' rescue nil
require 'nokogiri'

file_path = "your_page.html"
txt =  <<-EOF
    <p>This is a link: http://www.url1.com/</p>
    <p>http://www.url2.com/</p>
    <p><img src="http://www.url3.com/image.jpg"> something</p>
EOF
doc = Nokogiri::HTML txt
doc.css("img").each do |link|
  puts link
  link.attributes["src"].value = "REPLACED"
end

puts doc.to_s

# SECOND SOLUTION

require 'uri'
rtxt = txt.gsub URI.regexp do |match|
  "REPLACED"
end
puts rtxt
需要“cgi”
需要“rubygems”救援无
需要“nokogiri”
file\u path=“your\u page.html”

txt=您可能希望避免使用正则表达式进行HTML/XML攻击。请尝试使用nokogiri。请参阅

[更新]

[更新]

我已经对上述问题进行了重新处理,现在就是了

require 'cgi'
require 'rubygems' rescue nil
require 'nokogiri'

file_path = "your_page.html"
txt =  <<-EOF
    <p>This is a link: http://www.url1.com/</p>
    <p>http://www.url2.com/</p>
    <p><img src="http://www.url3.com/image.jpg"> something</p>
EOF
doc = Nokogiri::HTML txt
doc.css("img").each do |link|
  puts link
  link.attributes["src"].value = "REPLACED"
end

puts doc.to_s

# SECOND SOLUTION

require 'uri'
rtxt = txt.gsub URI.regexp do |match|
  "REPLACED"
end
puts rtxt
需要“cgi”
需要“rubygems”救援无
需要“nokogiri”
file\u path=“your\u page.html”
txt=我用rinku解决了这个问题(https://github.com/tanoku/rinku/tree/)

我用林库解决了这个问题(https://github.com/tanoku/rinku/tree/)


只需尝试以下代码,我相信您会得到所需的输出:

require 'uri'
text = '<p>This is a link: http://www.url1.com/</p>
<p>http://www.url2.com/</p>
<p><img src="http://www.url3.com/image.jpg"> something</p>'
URI.extract(text)
并且文本的输出是

"<p>This is a link : REPLACED</p>\n<p>REPLACED</p>\n<p><img src=\"REPLACED\"> something</p>"
“这是一个链接:替换的

\n替换的

\n某物


希望对您有所帮助。

只要尝试下面的代码,我相信您会得到所需的输出:

require 'uri'
text = '<p>This is a link: http://www.url1.com/</p>
<p>http://www.url2.com/</p>
<p><img src="http://www.url3.com/image.jpg"> something</p>'
URI.extract(text)
并且文本的输出是

"<p>This is a link : REPLACED</p>\n<p>REPLACED</p>\n<p><img src=\"REPLACED\"> something</p>"
“这是一个链接:替换的

\n替换的

\n某物


希望对您有所帮助。

谢谢您的建议。但是,我没有找到解析纯文本URL的方法(没有标记)在一个nokogiri中。我不明白。你发布的示例中有很多html,即标记。如果你有没有标记的URL,那么就用它将URI分解成碎片,并根据需要重新组装。我还添加了另一个stackoverflow问题,向你展示了如何做你想要的事情。添加了一个完整的解决方案,我在我的shell中为你测试过u:)再次更新了答案以使用URL.regexp函数,该函数返回URI的regexp。然后,您可以将其与gsub一起使用以替换所有URI。感谢您的建议。但是,我没有找到解析纯文本URL的方法(没有标记)在一个nokogiri中。我不明白。你发布的示例中有很多html,即标记。如果你有没有标记的URL,那么就用它将URI分解成碎片,并根据需要重新组装。我还添加了另一个stackoverflow问题,向你展示了如何做你想要的事情。添加了一个完整的解决方案,我在我的shell中为你测试过u:)再次更新了答案以使用URL.regexp函数,该函数返回URI的regexp。然后,您可以将该函数与gsub一起使用以替换所有URI。
links.shift => "link :"
links.each do |link|
  text = text.gusb(link, "REPLACED")
end
"<p>This is a link : REPLACED</p>\n<p>REPLACED</p>\n<p><img src=\"REPLACED\"> something</p>"