php-mf2 is a pure, generic microformats-2 parser for PHP. It makes HTML as easy to consume as JSON.
php-mf2 is a pure, generic microformats-2 parser for PHP. It makes HTML as easy to consume as JSON.
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.
[!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/mf2If 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:
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
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.
$doc = '
';
$parser = new Mf2\Parser($doc);
$parser->lang = true;
$result = $parser->parse();{
"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);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.
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:
e-* property is not HTML-escaped. For example, <code> will result in "name": [""]. At the very least, HTML-escape all properties before echoing them out in HTMLe-* properties, you SHOULD purify the HTML before displaying it to prevent injection of arbitrary code. For PHP we recommend using HTML PurifierIssues 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 :)
composer install../vendor/bin/phpunit./vendor/bin/phpunit) and that your code is compatible with all supported versions of PHP (./vendor/bin/phpcs -p)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
composer cs-checkcomposer testscomposer tests-no-internetcomposer microformats-tests-mf1composer microformats-tests-mf2composer microformats-tests-mixedcomposer check-and-testcomposer check-and-test-allphp-mf2 is dedicated to the public domain using Creative Commons -- CC0 1.0 Universal.
No open issues yet, or sync has not completed.