Skip to content

Instantly share code, notes, and snippets.

@jsit
Last active June 7, 2021 05:12
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsit/ca777a3ca71e725d9630 to your computer and use it in GitHub Desktop.
Save jsit/ca777a3ca71e725d9630 to your computer and use it in GitHub Desktop.
PHP preg_replace strings for AMP HTML extensions
$body = preg_replace( '/<iframe\b[^>]*soundcloud.com\/tracks\/(\d*).*"[^>]*>(.*?)>/', '<amp-soundcloud height=300 layout="fixed-height" data-trackid="$1" data-visual="true"></amp-soundcloud>', $body );
$body = preg_replace( '/<blockquote class="twitter-tweet"[^>]*>.*\n*.*twitter.com\/\w*\/status\/(\d*)".*<\/blockquote>/', '<amp-twitter width=486 height=657 layout="responsive" data-tweetid="$1"></amp-twitter>', $body );
$body = preg_replace( '/<iframe\b[^>]*vimeo.com\/video\/(\d*).*"[^>]*width="([^"]*)"[^>]*height="([^"]*)"[^>]*[^>]*>(.*?)>/', '<amp vimeo layout="responsive" width="$2" height="$3" data-videoid="$1"></amp-vimeo>', $body );
$body = preg_replace( '/<iframe\b[^>]*width="([^"]*)"[^>]*height="([^"]*)"[^>]*youtube.com\/embed\/([^"]*)[^>]*>(.*?)>/', '<amp-youtube width="$1" height="$2" data-videoid="$3" layout="responsive"></amp-youtube>', $body );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment