Atom And Iframes
Wednesday 16th December, 2015 10:34 Comments: 0
After embedding the Star Trek Beyond trailer into my website, which also required some adjustment of my recently added Content Security Policy header, I discovered that my Atom feed was no longer valid. I couldn't see anything obvious in the RFC about the iframe tag, but several feed validators insist that iframes are evil.
The iframe tag doesn't support fallback content, but I can replace the iframe with a link to the content. Someone's bound to have done that before, right? And provided some example PHP? Right?
After a bit of searching, I gave up and resorted to writing my own code. I've been a bit lazy as I'm assuming that the value of the src attribute will be surrounded by double quotes and that there aren't any spaces around the equals sign, which it doesn't have to be when writing valid HTML5 code, and that there's a closing iframe tag (which should be the case, as I don't think it's a self-closing element). Anyway, here's my code, including a quick str_replace to replace the embedded version of YouTube videos with the normal web page:
The iframe tag doesn't support fallback content, but I can replace the iframe with a link to the content. Someone's bound to have done that before, right? And provided some example PHP? Right?
After a bit of searching, I gave up and resorted to writing my own code. I've been a bit lazy as I'm assuming that the value of the src attribute will be surrounded by double quotes and that there aren't any spaces around the equals sign, which it doesn't have to be when writing valid HTML5 code, and that there's a closing iframe tag (which should be the case, as I don't think it's a self-closing element). Anyway, here's my code, including a quick str_replace to replace the embedded version of YouTube videos with the normal web page:
// replace iframe with a link $text = preg_replace('/<iframe.*?src="(.*?)".*?\<\/iframe>/', '<a href="$1">$1</a>', $text); // replace YouTube embed links with watch links $text = str_replace(".youtube.com/embed/", ".youtube.com/watch/", $text);I'll leave it to someone else to extend this to support single quotes and spaces. Or everyone could write their HTML my way. The right way.