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

php-mf2

> 数据库
开源

php-mf2 是一个纯粹的通用微格式 2 解析器,适用于 PHP。它使 HTML 的使用与 JSON 相当简单。

199 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

php-mf2 是一个纯粹的通用微格式 2 解析器,适用于 PHP。它使 HTML 的使用与 JSON 相当简单。

php-mf2

php-mf2 is a pure, generic microformats-2 parser. It makes HTML as easy to consume as JSON.

Instead of having a hard-coded list of all the different microformats, it follows a set of procedures to handle different property types (e.g. p- for plaintext, u- for URL, etc). This allows for a very small and maintainable parser.

Installation

[!NOTE]

This will be the last release that supports PHP 5.6 as a minimum version. The next release, 0.6.0, will require PHP 7.4 as a minimum.

There are two ways of installing php-mf2. We highly recommend installing php-mf2 using Composer. The rest of the documentation assumes that you have done so.

To install using Composer, run

composer require mf2/mf2

If you can’t or don’t want to use Composer, then php-mf2 can be installed the old way by downloading /Mf2/Parser.php, adding it to your project and requiring it from files you want to call its functions from, like this:

php
parseRelsAndAlternates()` instead of `->parse()`, e.g.

```php
parseFromId('parse-from-here'); // returns a document with only the h-card descended from div#parse-from-here

$elementIWant = $parser->query('an xpath query')[0];

$parser->parse(true, $elementIWant); // returns a document with only the Microformats under the selected element

Experimental Language Parsing

There is still ongoing brainstorming around how HTML language attributes should be added to the parsed result. In order to use this feature, you will need to set a flag to opt in.

php
$doc = '

  

';
$parser = new Mf2\Parser($doc);
$parser->lang = true;
$result = $parser->parse();
json
{
  "items": [
    {
      "type": ["h-entry"],
      "properties": {
        "name": ["En svensk titel"],
        "content": [
          {
            "html": "With an english summary",
            "value": "With an english summary",
            "lang": "en"
          },
          {
            "html": "Och svensk huvudtext",
            "value": "Och svensk huvudtext",
            "lang": "sv"
          }
        ]
      },
      "lang": "sv"
    }
  ],
  "rels": {},
  "rel-urls": {}
}

…

php
// …by passing true as the third constructor:
$jsonParser = new Mf2\Parser($html, $url, true);

Classic Microformats Markup

php-mf2 has some support for parsing classic microformats markup. It’s enabled by default, but can be turned off by calling Mf2\parse($html, $url, false); or $parser->parse(false); if you’re instantiating a parser yourself.

If the built in mappings don’t successfully parse some classic microformats markup, please raise an issue and we’ll fix it.

Security

No filtering of content takes place in mf2\Parser, so treat its output as you would any untrusted data from the source of the parsed document.

Some tips:

  • All content apart from the 'html' key in dictionaries produced by parsing an e-* property is not HTML-escaped. For example, &lt;code&gt; will result in "name": [""]. At the very least, HTML-escape all properties before echoing them out in HTML
  • If you’re using the raw HTML content under the 'html' key of dictionaries produced by parsing e-* properties, you SHOULD purify the HTML before displaying it to prevent injection of arbitrary code. For PHP we recommend using HTML Purifier

Contributing

Issues and bug reports are very welcome. If you know how to write tests then please do so as code always expresses problems and intent much better than English, and gives me a way of measuring whether or not fixes have actually solved your problem. If you don’t know how to write tests, don’t worry :) Just include as much useful information in the issue as you can.

Pull requests very welcome, please try to maintain stylistic, structural and naming consistency with the existing codebase, and don’t be too upset if we make naming changes :)

How to make a Pull Request

  1. Fork the repo to your github account
  2. Clone a copy to your computer (simply installing php-mf2 using composer only works for using it, not developing it)
  3. Install the dev dependencies with composer install.
  4. Run PHPUnit with ./vendor/bin/phpunit
  5. Add PHPUnit tests for your changes, either in an existing test file if suitable, or a new one
  6. Make your changes
  7. Make sure your tests pass (./vendor/bin/phpunit) and that your code is compatible with all supported versions of PHP (./vendor/bin/phpcs -p)
  8. Go to your fork of the repo on github.com and make a pull request, preferably with a short summary, detailed description and references to issues/parsing specs as appropriate
  9. Bask in the warm feeling of having contributed to a piece of free software (optional)

Testing

Run composer install and composer check-and-tests. This will run phpcs, the php-mf2 tests, and the cross-platform microformats/tests (excluding mf1; more details below).

The php-mf2 tests are in /tests/Mf2 and contain many parsing examples, internal parser tests, and regression tests for specific issues over its history. If you need to run tests without an internet connection, run composer tests-no-internet.

microformats/tests are included via MicroformatsTestSuiteTest.php. It has tests for mf2 and mf1. php-mf2 will fail some of the mf1 tests, so the check-and-tests script will skip them currently. It is a goal to pass those mf1 tests, so you can run them with composer microformats-tests-mf1. There are additional composer scripts listed below for running parts of this test suite on their own.

Composer scripts

  • phpcs: run with composer cs-check
  • php-mf2 tests + microformats/tests, except mf1: run with composer tests
    • same, but exclude tests that require internet: composer tests-no-internet
  • microformats/tests suite:
    • run mf1 tests with composer microformats-tests-mf1
    • run mf2 tests with composer microformats-tests-mf2
    • run mixed tests with composer microformats-tests-mixed
  • phpcs + php-mf2 tests + microformats/tests, except mf1: run composer check-and-test
  • phpcs + all tests: run composer check-and-test-all

License

php-mf2 is dedicated to the public domain using Creative Commons -- CC0 1.0 Universal.

http://creativecommons.org/publicdomain/zero/1.0

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

PHPindiewebmicroformatsmicroformats2parser

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

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库