Jump to content

Iframe tags Regex


trogster

Recommended Posts

Hi, I'm trying to parse iframe tags from html.

I have this pattern so far:

'#<iframe[^>]*>.*?</iframe>#i'

 

This works ok for something like:

<iframe src='http://www.example.com' width='XXX' height='XXX'></iframe>
and
<iframe src='http://www.example.com' width='XXX' height='XXX'><a href="http://www.example.com">iframes not supported</a></iframe>

 

But I want to parse also iframe tags like this one:

<iframe src='http://www.example.com' width='XXX' height='XXX' />

 

Does anyone knows how to parse that last iframe code (and the previous) in the same pattern?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/179065-iframe-tags-regex/
Share on other sites

Cool, that one seems to work ok, thank you very much.

I had also made a small mod in my pattern and I think it's working too.

 

'#<iframe[^>]*>.*?(</iframe>)?#i'

 

I tested both with 3 different iframe tags and they worked fine.

I will add it some \s* to parse when there are spaces between < > and the tag text.

Link to comment
https://forums.phpfreaks.com/topic/179065-iframe-tags-regex/#findComment-944754
Share on other sites

I've made some tests and I still can't get what I need with any of both patterns :P

 

Here are the iframes I am using for testing:

<iframe src='http://google.com' framespacing='0' frameborder='no' scrolling='no' width='160' height='600' />
<iframe src='http://google.com' framespacing='0' frameborder='no' scrolling='no' width='160' height='600'>
<iframe src='http://google.com' framespacing='0' frameborder='no' scrolling='no' width='160' height='600'><a href="">iframes not supported</a></iframe>
<iframe src='http://google.com' framespacing='0' frameborder='no' scrolling='no' width='160' height='600'></iframe>

 

This pattern:

#(?:<iframe[^>]*)(??:/>)|(?:>.*?</iframe>))#i

Fails only on the second iframe (it doesn't parse anything there).

 

And this pattern:

#<iframe[^>]*>.*?(</iframe>)?#i

Fails on the third iframe (it parses everything until ...><a href.... that red char.)

 

UPDATE: I don't know how I didn't see the first and second iframe is an invalid html code :P I really tought iframe tags could be closed like img tags.

I think cags pattern will work ok.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/179065-iframe-tags-regex/#findComment-944779
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.