Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/folour/regex

Simple and useful abstraction over PHP preg_* functions for PHP 7.1
https://github.com/folour/regex

php71 regex regexp regexp-match regexp-search regular-expressions

Last synced: about 1 month ago
JSON representation

Simple and useful abstraction over PHP preg_* functions for PHP 7.1

Awesome Lists containing this project

README

        

# Regex
Simple and useful abstraction over PHP preg_* functions for PHP 7.1

### Implemented functions
- preg_split
- preg_match
- preg_match_all
- preg_replace
- preg_replace_callback

### Installation
```php
composer require folour/regex 'v1.0.0'
```

### Usage
```php
replace('/test/i', 'replaced');
//Converts to string and print text 'replaced [string], replaced [value]'
echo $replaced;

//fluent replacement
$replaced = $re
->replace('/test/i', 'replaced')
->replace('/replaced/', 'double_replaced');

echo $replaced;//'double_replaced [string], double_replaced [value]

//callback replacement
$replaced = $re->replace('/\[([a-z]+)\]/i', function($matches) {
return sprintf('[replaced_%s]', $matches[1]);
});
echo $replaced; //'Test [replaced_string], test [replaced_value]'

/*
* find matches
*/
//first match
$m = $re->find('/\[(?P[a-z]+)\]/');
var_dump($m);
// array(
// 'matched' => 'string'
// )

//all matches
$m = $re->findAll('/\[(?P[a-z]+)\]/');
var_dump($m);
// array(
// 0 => array(
// 'matched' => 'string'
// ),
// 1 => array(
// 'matched' => 'value'
// )
// )

/*
* Split string
*/
$parts = $re->split('/\,\s?/');
var_dump($parts);
// array(
// 0 => 'Test [string]',
// 1 => 'test [value]'
// )
```