PHPackages                             yuanqing/extract - 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. yuanqing/extract

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

yuanqing/extract
================

Sugar for getting data out of strings in PHP.

v0.1.0(11y ago)21801MITPHPPHP &gt;=5.3

Since Jul 19Pushed 11y ago1 watchersCompare

[ Source](https://github.com/yuanqing/extract)[ Packagist](https://packagist.org/packages/yuanqing/extract)[ Docs](https://github.com/yuanqing/extract)[ RSS](/packages/yuanqing-extract/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)DependenciesVersions (2)Used By (1)

Extract.php [![Packagist Version](https://camo.githubusercontent.com/2c38372480427cdbca069027dbdb71cb0070eaa5c4ee3b17ccdeddc27fbec7d5/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7975616e71696e672f657874726163742e737667)](https://packagist.org/packages/yuanqing/extract) [![Build Status](https://camo.githubusercontent.com/0a218c13d85394fc1266a05697a56a787c872200ecb7f5d43527bdc38ac1ca70/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7975616e71696e672f657874726163742e737667)](https://travis-ci.org/yuanqing/extract) [![Coverage Status](https://camo.githubusercontent.com/22d27e94552eed28b2817887a73d7b3a0e256310747961e29d6a2b77aef606cb/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f7975616e71696e672f657874726163742e737667)](https://coveralls.io/r/yuanqing/extract)
=======================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#extractphp---)

Regex sugar for getting data out of strings:

```
use yuanqing\Extract\Extract;

$e = new Extract('{{ foo.bar }}, {{ foo.baz }}!');
$e->extract('Hello, World!'); #=> ['foo' => ['bar' => 'Hello', 'baz' => 'World']]
```

Boom.

Usage
-----

[](#usage)

1. If the given string does not match the required format, `null` is returned.
2. Each capturing group is enclosed in double braces. Within said braces, we have:

    1. The name of the capturing group
    2. *(optional)* A character length
    3. *(optional)* A type specifier
3. A capturing group can be an arbitrary **string** (`s`):

    ```
    $e = new Extract('{{ foo: s }}, {{ bar: s }}!');
    $e->extract('Hello, World!'); #=> ['foo' => 'Hello', 'bar' => 'World']
    $e->extract('Hola, World!'); #=> ['foo' => 'Hola', 'bar' => 'World']

    $e = new Extract('{{ foo: 5s }}, {{ bar: 5s }}!');
    $e->extract('Hello, World!'); #=> ['foo' => 'Hello', 'bar' => 'World']
    $e->extract('Hola, World!'); #=> null
    ```
4. ...or an **integer** (`d`):

    ```
    $e = new Extract('{{ day: d }}-{{ month: d }}-{{ year: d }}');
    $e->extract('31-12-2014'); #=> ['day' => 31, 'month' => 12, 'year' => 2014]
    $e->extract('31-12-14'); #=> ['day' => 31, 'month' => 12, 'year' => 14]
    $e->extract('31-Dec-2014'); #=> null

    $e = new Extract('{{ day: 2d }}-{{ month: 2d }}-{{ year: 4d }}');
    $e->extract('31-12-2014'); #=> ['day' => 31, 'month' => 12, 'year' => 2014]
    $e->extract('31-12-14'); #=> null
    ```
5. ...or a **float** (`f`):

    ```
    $e = new Extract('{{ tau: f }}, {{ pi: f }}');
    $e->extract('6.28, 3.14'); #=> ['tau' => 6.28, 'pi' => 3.14]
    $e->extract('tau, pi'); #=> null

    $e = new Extract('{{ tau: 1.f }}, {{ pi: 1.f }}');
    $e->extract('6.28, 3.14'); #=> ['tau' => 6.28, 'pi' => 3.14]
    $e->extract('06.28, 03.14'); #=> null

    $e = new Extract('{{ tau: .2f }}, {{ pi: .2f }}');
    $e->extract('6.28, 3.14'); #=> ['tau' => 6.28, 'pi' => 3.14]
    $e->extract('6.283, 3.142'); #=> null

    $e = new Extract('{{ tau: 1.2f }}, {{ pi: 1.2f }}');
    $e->extract('6.28, 3.14'); #=> ['tau' => 6.28, 'pi' => 3.14]
    $e->extract('6.3, 3.1'); #=> null
    ```

All the examples in this README are in [the examples.php file](https://github.com/yuanqing/extract/blob/master/examples.php). You can also find more usage examples in [the tests](https://github.com/yuanqing/extract/tree/master/test).

Requirements
------------

[](#requirements)

Extract.php requires at least **PHP 5.3**, or **HHVM**.

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

[](#installation)

### Install with Composer

[](#install-with-composer)

1. Install [Composer](http://getcomposer.org/).
2. Install [the Extract.php Composer package](https://packagist.org/packages/yuanqing/extract):

    ```
    $ composer require yuanqing/extract ~0.1

    ```
3. In your PHP, require the Composer autoloader:

    ```
    require_once __DIR__ . '/vendor/autoload.php';
    ```

### Install manually

[](#install-manually)

1. Clone this repository:

    ```
    $ git clone https://github.com/yuanqing/extract

    ```

    Or just [grab the zip](https://github.com/yuanqing/extract/archive/master.zip).
2. In your PHP, require [Extract.php](https://github.com/yuanqing/extract/blob/master/src/Extract.php):

    ```
    require_once __DIR__ . '/src/Extract.php';
    ```

Testing
-------

[](#testing)

You need [PHPUnit](http://phpunit.de/) to run the tests:

```
$ git clone https://github.com/yuanqing/extract
$ cd extract
$ phpunit

```

License
-------

[](#license)

MIT license

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4312d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5aebd62c372779a1223fca7819cdd898e82698e011b7617aaf822042710a55ec?d=identicon)[yuanqing](/maintainers/yuanqing)

---

Tags

stringregexextractionextract

### Embed Badge

![Health badge](/badges/yuanqing-extract/health.svg)

```
[![Health](https://phpackages.com/badges/yuanqing-extract/health.svg)](https://phpackages.com/packages/yuanqing-extract)
```

###  Alternatives

[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k295.3M2.5k](/packages/symfony-property-access)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[composer/pcre

PCRE wrapping library that offers type-safe preg\_\* replacements.

693313.8M34](/packages/composer-pcre)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[marquine/php-etl

Extract, Transform and Load data using PHP.

182137.5k](/packages/marquine-php-etl)[opis/string

Multibyte strings as objects

7120.9M7](/packages/opis-string)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
