PHPackages                             jeffpacks/substractor - 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. jeffpacks/substractor

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

jeffpacks/substractor
=====================

A sub-string extractor for the regex ignorant

2.2.0(2y ago)17.0k↓50%1MITPHPPHP ^7.4|^8.0

Since Mar 12Pushed 2y ago1 watchersCompare

[ Source](https://github.com/jeffpacks/substractor)[ Packagist](https://packagist.org/packages/jeffpacks/substractor)[ Docs](https://github.com/jeffpacks/substractor)[ RSS](/packages/jeffpacks-substractor/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (8)Used By (1)

About
=====

[](#about)

Substractor is a utility for matching strings against wildcard patterns and for extracting or manipulating substrings according to macro patterns. All pattern formats are easy to understand, suitable for simple string matching, extraction and manipulation tasks without the need for those wonderful and pesky regular expressions.

Dependencies
============

[](#dependencies)

This library requires at least PHP 7.4.

Installing
==========

[](#installing)

Composer
--------

[](#composer)

Run `composer require jeffpacks/substractor` in your project's root directory.

Downloading
-----------

[](#downloading)

`git clone https://github.com/jeffpacks/substractor.git` or download from `https://github.com/jeffpacks/substractor/archive/refs/heads/master.zip`.

Add `require('/path/to/substractor/autoload.php')` to your PHP script, and you're good to go.

Patterns
========

[](#patterns)

Substractor uses only two types of wildcard tokens in its patterns:

- `*` matches any substring of zero or more characters
- `?` matches any single character

You can then build patterns like these and match them against strings:

- `http*://example.test` matches `http://example.test` and `https://example.test`
- `https://example.???` matches `https://example.com` but not `https://example.io`

The wildcard tokens are used for matching, but can also be used for extracting substrings.

A key feature of Substractor's extraction algorithms is that they are word bound. They will look for substrings in "words" (substrings delimited by whitespace characters), rather than across the entire string, giving you `foobar` and not `foobar gone?` when looking for `foo*` in `Where has the foobar gone?`

Macros
======

[](#macros)

A macro is a named substring or a named set of substrings. By including macro tokens in your patterns, you can extract named substrings. A macro named `foo` looks like this in a pattern: `{foo}`. Extracting the protocol, domain and top-level domain of a URL would require a pattern with macro tokens like this: `{protocol}://{domain}.{top}*`

The methods
===========

[](#the-methods)

Substractor::matches()
----------------------

[](#substractormatches)

This method indicates whether a given string matches a pattern that you specify. Here' an example:

```
