2017 © Pedro Peláez
 

library extract

Sugar for getting data out of strings in PHP.

image

yuanqing/extract

Sugar for getting data out of strings in PHP.

  • Saturday, July 19, 2014
  • by yuanqing
  • Repository
  • 1 Watchers
  • 2 Stars
  • 154 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Extract.php Packagist Version Build Status Coverage Status

Regex sugar for getting data out of strings:, (*1)

use yuanqing\Extract\Extract;

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

Boom., (*2)

Usage

  1. If the given string does not match the required format, null is returned., (*3)

  2. Each capturing group is enclosed in double braces. Within said braces, we have:, (*4)

    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):, (*5)

    $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):, (*6)

    $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):, (*7)

    $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. You can also find more usage examples in the tests., (*8)

Requirements

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

Installation

Install with Composer

  1. Install Composer., (*10)

  2. Install the Extract.php Composer package:, (*11)

    $ composer require yuanqing/extract ~0.1
    
  3. In your PHP, require the Composer autoloader:, (*12)

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

Install manually

  1. Clone this repository:, (*13)

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

    Or just grab the zip., (*14)

  2. In your PHP, require Extract.php:, (*15)

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

Testing

You need PHPUnit to run the tests:, (*16)

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

License

MIT license, (*17)

The Versions

19/07 2014

dev-master

9999999-dev https://github.com/yuanqing/extract

Sugar for getting data out of strings in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Lim Yuan Qing

extraction extract string regex

19/07 2014

v0.1.0

0.1.0.0 https://github.com/yuanqing/extract

Sugar for getting data out of strings in PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Lim Yuan Qing

extraction extract string regex