0
p.content
 => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras gravida quis orci sed pellentesque. Nam ullamcorper. Proin eget dapibus tellus. <br /><br /><iframe title=\"title\" src=\"http://www.youtube.com/embed/yt_video_id\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"\"></iframe>"

I would like to know if is possible check if inside of p.content text exist or not a youtube iframe.

toro2k
  • 19,020
  • 7
  • 64
  • 71
hyperrjas
  • 10,666
  • 25
  • 99
  • 198

2 Answers2

2

There aren't so many choises, I think; you have to parse the string and check if the parsed object is what you expect, for example:

require 'nokogiri'

fragment = Nokogiri::HTML.fragment(p.content)
fragment.css('iframe[src*="www.youtube.com"]').any?
# => true

fragment.css('iframe[src*="www.google.com"]').any?
# => false
toro2k
  • 19,020
  • 7
  • 64
  • 71
0

You can check if a string has a substring containing some text using the include? method:

p.content.include? "iframe"  

Here are the docs for include?, http://ruby-doc.org/core-2.1.0/String.html#method-i-include-3F

MurifoX
  • 14,991
  • 3
  • 36
  • 60