Everything, Everything

2024: January February March April
2023: J F M A M J J A S O N D
2022: J F M A M J J A S O N D
2021: J F M A M J J A S O N D
2020: J F M A M J J A S O N D
2019: J F M A M J J A S O N D
2018: J F M A M J J A S O N D
2017: J F M A M J J A S O N D
2016: J F M A M J J A S O N D
2015: J F M A M J J A S O N D
2014: J F M A M J J A S O N D
2013: J F M A M J J A S O N D
2012: J F M A M J J A S O N D
2011: J F M A M J J A S O N D
2010: J F M A M J J A S O N D
2009: J F M A M J J A S O N D
2008: J F M A M J J A S O N D
2007: J F M A M J J A S O N D
2006: J F M A M J J A S O N D
2005: J F M A M J J A S O N D
2004: J F M A M J J A S O N D
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:
// 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.
© Robert Nicholls 2002-2024
The views and opinions expressed on this site do not represent the views of my employer.
HTML5 / CSS3