Skip to content

Instantly share code, notes, and snippets.

Last active October 3, 2022 06:26
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Make a chunk of markup AMP-ready
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
function ampify($html='') {
# Replace img, audio, and video elements with amp custom elements
$html = str_ireplace(
# Add closing tags to amp-img custom element
$html = preg_replace('/<amp-img(.*?)>/', '<amp-img$1></amp-img>',$html);
# Whitelist of HTML tags allowed by AMP
$html = strip_tags($html,'<h1><h2><h3><h4><h5><h6><a><p><ul><ol><li><blockquote><q><cite><ins><del><strong><em><code><pre><svg><table><thead><tbody><tfoot><th><tr><td><dl><dt><dd><article><section><header><footer><aside><figure><time><abbr><div><span><hr><small><br><amp-img><amp-audio><amp-video><amp-ad><amp-anim><amp-carousel><amp-fit-rext><amp-image-lightbox><amp-instagram><amp-lightbox><amp-twitter><amp-youtube>');
return $html;
Copy link

How it works ?.

Copy link

thanks , it helped alot .

Copy link

I Googled' it and found this for anyone wanting to use it for WordPress.

Nice job Adactio, seems like you got the credit too!

Copy link

^^ Is there a node.js equivalent to this? I need it for my Gruntfile. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment