Skip to content

Instantly share code, notes, and snippets.

@adactio
Last active October 3, 2022 06:26

Revisions

  1. adactio revised this gist Oct 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ampify.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@

    function ampify($html='') {

    # Replace img, iframe, audio, and video with amp custom elements
    # Replace img, audio, and video elements with amp custom elements
    $html = str_ireplace(
    ['<img','<video','/video>','<audio','/audio>'],
    ['<amp-img','<amp-video','/amp-video>','<amp-audio','/amp-audio>'],
  2. adactio revised this gist Oct 11, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ampify.php
    Original file line number Diff line number Diff line change
    @@ -7,16 +7,16 @@ function ampify($html='') {

    # Replace img, iframe, audio, and video with amp custom elements
    $html = str_ireplace(
    ['<img','<video','/video>','<iframe','/iframe>','<audio','/audio>'],
    ['<amp-img','<amp-video','/amp-video>','<amp-iframe','/amp-iframe>','<amp-audio','/amp-audio>'],
    ['<img','<video','/video>','<audio','/audio>'],
    ['<amp-img','<amp-video','/amp-video>','<amp-audio','/amp-audio>'],
    $html
    );

    # 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-iframe><amp-video><amp-anim><amp-carousel><amp-fit-rext><amp-image-lightbox><amp-instagram><amp-lightbox><amp-twitter><amp-youtube>');
    $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;

  3. adactio revised this gist Oct 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ampify.php
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ function ampify($html='') {
    );

    # Add closing tags to amp-img custom element
    $html = preg_replace('/(<amp-img("[^"]*"|[^">])*)>/', '$1></amp-img>',$html);
    $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-iframe><amp-video><amp-anim><amp-carousel><amp-fit-rext><amp-image-lightbox><amp-instagram><amp-lightbox><amp-twitter><amp-youtube>');
  4. adactio created this gist Oct 10, 2015.
    25 changes: 25 additions & 0 deletions ampify.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?php

    # Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
    # http://creativecommons.org/publicdomain/zero/1.0/

    function ampify($html='') {

    # Replace img, iframe, audio, and video with amp custom elements
    $html = str_ireplace(
    ['<img','<video','/video>','<iframe','/iframe>','<audio','/audio>'],
    ['<amp-img','<amp-video','/amp-video>','<amp-iframe','/amp-iframe>','<amp-audio','/amp-audio>'],
    $html
    );

    # Add closing tags to amp-img custom element
    $html = preg_replace('/(<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-iframe><amp-video><amp-anim><amp-carousel><amp-fit-rext><amp-image-lightbox><amp-instagram><amp-lightbox><amp-twitter><amp-youtube>');

    return $html;

    }

    ?>