百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

amp-library

> 编程语言
开源

将 HTML 转换为 AMP HTML,并报告 HTML 是否符合 AMP HTML 规范

379 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

将 HTML 转换为 AMP HTML,并报告 HTML 是否符合 AMP HTML 规范

AMP PHP Library

An open source PHP library and console utility to convert HTML to AMP HTML and report HTML compliance with the AMP HTML specification.

What is the AMP PHP Library?

The AMP PHP Library is an open source and pure PHP Library that:

  • Works with whole or partial HTML documents (or strings). Specifically, the AMP PHP Library:
  • Reports compliance of a whole/partial HTML document with the AMP HTML specification. We implement an AMP HTML validator in pure PHP to report compliance of an arbitrary HTML document / HTML fragment with the AMP HTML standard. This validator is a ported subset of the canonical validator that is implemented in JavaScript
    • Specifically, the PHP validator supports tag specification validation, attribute specification validation, CDATA validation, CSS validation, layout validation, template validation and attribute property-value pair validation. It will report tags and attributes that are missing, illegal, mandatory according to spec but not present, unique according to spec but multiply present, having wrong parents or ancestors or children and so forth.
    • Note: while the AMP PHP library (already) supports many of the features and capabilities of the canonical validator, it is not intended to achieve parity in every respect with the canonical validator. Even within the features we support (e.g. CSS validation) there may be certain validation issues that we don't flag but the canonical validator does.
  • Using the feedback given by the in-house PHP validator, the AMP PHP library tries to "correct" some issues found in the HTML to make it more AMP HTML compliant. This would, for example, involve:
    • Removing illegal attributes e.g. style attribute within `` tag
    • Removing all kinds of illegal tags e.g. `

Line 9:

Line 10:

Transformations made from HTML tags to AMP custom tags

at line 3 ACTION TAKEN: img tag was converted to the amp-img tag.

at line 7 ACTION TAKEN: iframe tag was converted to the amp-iframe tag.

AMP-HTML Validation Issues and Fixes

FAIL

on line 1

  • The attribute 'style' may not appear in tag 'a'. [code: DISALLOWED_ATTR category: DISALLOWED_HTML] ACTION TAKEN: a.style attribute was removed due to validation issues.
  • Invalid URL protocol 'javascript:' for attribute 'href' in tag 'a'. [code: INVALID_URL_PROTOCOL category: DISALLOWED_HTML] ACTION TAKEN: a.href attribute was removed due to validation issues.

on line 2

  • The attribute 'style' may not appear in tag 'a'. [code: DISALLOWED_ATTR category: DISALLOWED_HTML] ACTION TAKEN: a.style attribute was removed due to validation issues.
  • The attribute 'target' in tag 'a' is set to the invalid value '_parent'. [code: INVALID_ATTR_VALUE category: DISALLOWED_HTML] ACTION TAKEN: a.target attribute was removed due to validation issues.

on line 5

  • The tag 'input' is disallowed. [code: DISALLOWED_TAG category: DISALLOWED_HTML] ACTION TAKEN: input tag was removed due to validation issues.

paragraph ';

// Load up the HTML into the AMP object // Note that we only support UTF-8 or ASCII string input and output. (UTF-8 is a superset of ASCII) $amp->loadHtml($html);

// If you're feeding it a complete document use the following line instead // $amp->loadHtml($html, ['scope' => Scope::HTML_SCOPE]);

// If you want some performance statistics (see https://github.com/Lullabot/amp-library/issues/24) // $amp->loadHtml($html, ['add_stats_html_comment' => true]);

// Convert to AMP HTML and store output in a variable $amp_html = $amp->convertToAmpHtml();

// Print AMP HTML print($amp_html);

// Print validation issues and fixes made to HTML provided in the $html string print($amp->warningsHumanText());

// warnings that have been passed through htmlspecialchars() function // print($amp->warningsHumanHtml());

// You can do the above steps all over again without having to create a fresh object // $amp->loadHtml($another_string) // ... // ...


### Tips
- Its probably not a good idea to run the library on your HTML dynamically on _every_ page view. You should try caching the results of `$amp->convertToAmpHtml()` once the library has run. If you're using the library from a CMS then you should consider using the caching facilities provided by the CMS.

### Caveats and Known issues
- We only support UTF-8 string input and output from the library. If you're using ASCII, then you don't need to worry as UTF-8 is a superset of ASCII. If you're using another encoding like Latin-1 (etc.) you'll need to convert to UTF-8 strings before you use this library
- If you have ``s with `https` urls _and_ they don't have height/width attributes _and_ you are using PHP 5.6 or higher _and_ you have not listed any certificate authorities (`cafile`) in your `php.ini` file  _then_ the library may have problems converting these to ``. This is because of http://php.net/manual/en/migration56.openssl.php . That link also has a work around.
- If your `` pins are appearing "chopped off" (after pinterest embed code conversion) try the workaround [here](https://github.com/Lullabot/amp-library/issues/46#issuecomment-230424580)

### Useful Links
- [Composer homepage](https://packagist.org/packages/lullabot/amp) for the AMP PHP Library on [Packagist](https://packagist.org/), the PHP package repository
- AMP Project [Homepage](https://www.ampproject.org/)
- AMP Project [code repository](https://github.com/ampproject/amphtml) on Github
- [AMP HTML JavaScript validator subtree](https://github.com/ampproject/amphtml/tree/main/validator) on Github within the AMP Project code repository
- [Technical Specification](https://github.com/ampproject/amphtml/blob/main/validator/validator-main.protoascii) of AMP HTML in [Protocol Buffers](https://developers.google.com/protocol-buffers/) ASCII message format. See [here](https://github.com/ampproject/amphtml/blob/main/validator/validator.proto) for the Schema definition of the technical specification

### Useful Links for amp-library developers

- [Notes](https://github.com/Lullabot/amp-library/blob/main/src/Spec/README.md) on the contents of the `src/Spec` folder
- [Notes](https://github.com/Lullabot/amp-library/blob/main/src/Validate/README.md) on the contents of the `src/Validate` folder

You can ignore these links if you simply plan to _use_ this library and not develop for it

### Third-party libraries

- Symfony:
    - [takeit/amp-html-bundle](https://github.com/takeit/AmpHtmlBundle)

- Drupal:
    - [Drupal AMP Module](https://www.drupal.org/project/amp)

### Sponsored by

- Google for creating the AMP Project and sponsoring development
- Lullabot for development of the module, theme, and library to work with the specifications

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言