Skip to content

Instantly share code, notes, and snippets.

@williankeller
Last active February 18, 2024 12:46
Show Gist options
  • Save williankeller/6fd7b6a161ef28c7794f940b4538ac05 to your computer and use it in GitHub Desktop.
Save williankeller/6fd7b6a161ef28c7794f940b4538ac05 to your computer and use it in GitHub Desktop.
Convert HTML to AMP
<?php
class AMP {
private $html;
/**
* HtmlToAmp constructor.
*/
public function __construct($htmlContent) {
$this->html = $htmlContent;
}
public function convertedHTML() {
echo $this->ampify();
}
/**
*
*/
private function replaceTagsMain() {
// Replacing all img, audio, iframe, video elements to amp custom elements.
$this->html = str_ireplace(
['<html', 'https:', 'http:', '<img', '<video', '/video>', '<audio', '/audio>'], ['<html amp', '', '', '<amp-img', '<amp-video', '/amp-video>', '<amp-audio', '/amp-audio>'], $this->html
);
}
private function replaceHeaderMetas() {
// Content to replace header.
$this->html = preg_replace('/<(meta|link) ((charset|property|http-equiv)=|(name|rel)=[\\"](viewport|canonical)[\\"])(.*?)>/', '', $this->html);
}
private function replaceHeaderMain() {
// Content to replace header.
$this->html = preg_replace(
'/<head\/?>(.*)<\/head>/s', '<head>'
. '<meta charset="utf-8">'
. '<script async src="https://cdn.ampproject.org/v0.js"></script>'
. '$1'
. '</head>', $this->html);
}
private function replaceHeaderTags() {
// Content to replace header.
$this->html = preg_replace(
'/<head\/?>(.*)<\/head>/s', '<head>'
. '$1'
. '<link rel="canonical" href="$SOME_URL" />'
. '<meta name="viewport" content="width=device-width,minimum-scale=1">'
. '</head>', $this->html);
}
private function replaceHeaderStyles() {
// Content to replace header.
$this->html = preg_replace(
'/<head\/?>(.*?)<\/head>/s', '<head>'
. '$1'
. '<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>'
. '<noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>'
. '</head>', $this->html);
}
private function replaceBodyContent() {
// Adding amp-img closing tag.
$this->html = preg_replace('/<amp-img(.*?)\/?>/', '<amp-img layout="responsive"$1></amp-img>', $this->html);
}
protected function ampify() {
// Content to replace header.
$this->replaceHeaderMetas();
// Replacing all img, audio, iframe, video elements to amp custom elements.
$this->replaceTagsMain();
// Content to replace header.
$this->replaceHeaderMain();
// Content to replace header.
$this->replaceHeaderTags();
// Content to replace header.
$this->replaceHeaderStyles();
// Adding amp-img closing tag.
$this->replaceBodyContent();
return $this->html;
}
}
$html = file_get_contents('http://www.contentsuit.com');
$amp = new AMP($html);
$amp->convertedHTML();
@jagg1973
Copy link

Hello,
I am learning about converting to amp my static ste.
How do I use this tool?
Thanks

@williankeller
Copy link
Author

Hi, @jagg1973.
This is an automatic converter PHP-based. You have to have PHP running in order to use this tool.
You have an MVC using PHP you just have to call it within a controller.
if you have a static HTML website, you can run it manually, like that:

$html = file_get_contents('YOUR_WEBSITE_URL');

$amp = new AMP($html);

$amp->convertedHTML();

Change YOUR_WEBSITE_URL to your website URL.

@shoaiyb
Copy link

shoaiyb commented Jul 25, 2021

lang attribute and meta tags have been removed after the conversion.

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