PHPackages                             alleyinteractive/wp-match-blocks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. alleyinteractive/wp-match-blocks

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alleyinteractive/wp-match-blocks
================================

Match WordPress blocks in the given content.

v4.3.0(5mo ago)9345.6k↑14.3%[2 issues](https://github.com/alleyinteractive/wp-match-blocks/issues)3GPL-2.0-or-laterPHPPHP ^8.2

Since Aug 23Pushed 5mo ago22 watchersCompare

[ Source](https://github.com/alleyinteractive/wp-match-blocks)[ Packagist](https://packagist.org/packages/alleyinteractive/wp-match-blocks)[ RSS](/packages/alleyinteractive-wp-match-blocks/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (14)Used By (3)

Match Blocks
============

[](#match-blocks)

`match_blocks()` selects the blocks in post content, or in a given set of blocks, inner blocks, or block HTML, that match the given criteria, such as the block name, block attributes, or position within the set of blocks.

Blocks can be matched by:

- Block name or names (`name`)
- Block attributes (`attrs`, `with_attrs`)
- Block inner HTML (`with_innerhtml`)
- The block's positive or negative index within the set (`position`)
- Whether the block represents only space (`skip_empty_blocks`)
- Whether the block has inner blocks (`has_innerblocks`)
- Custom validation classes (`is_valid`)
- Xpath queries (`__experimental_xpath`) ([huh?](#matching-blocks-with-xpath))

Passing matching parameters is optional; all non-empty blocks match by default.

Additionally:

- Recursion into inner blocks is supported (`flatten`).
- The set of matching blocks can be limited by size (`limit`) or their position in the set of matches (`nth_of_type`).
- The number of matches can be returned instead of the matched blocks (`count`).
- The companion `match_block()` function reduces the filtered set of results to a single parsed block.
- Passing a single block instance will return matches from its inner blocks.

`match_blocks()` is powered by a set of block validation classes that utilize the [Laminas Validator](https://docs.laminas.dev/laminas-validator/) framework and [Laminas Validator Extensions](https://github.com/alleyinteractive/laminas-validator-extensions) package. These validators, along with a base class for validating blocks, are included here. [See the validators section for their documentation](#validators).

Installation
------------

[](#installation)

Install the latest version with:

```
composer require alleyinteractive/wp-match-blocks
```

Basic usage
-----------

[](#basic-usage)

Find all paragraph blocks in a post:

```
