PHP Classes

PHP String Match Variables: Extract values from strings using tag patterns

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-06-17 (5 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 43 All time: 10,704 This week: 116Up
Version License PHP version Categories
string-vars 1.0.0MIT/X Consortium ...5Text processing, PHP 8


This package can extract values from strings using tag patterns.

It can take a string as a parameter that defines the format to match.

The format string can have tags that define each value's format and the value's name to extract.

The package can check if another string matches the format defined initially, and then it extracts the tagged values into an associative array with values named using the tag names.

Picture of Mateo
  Performance   Level  
Name: Mateo <contact>
Classes: 17 packages by
Country: Peru Peru
Age: ???
All time rank: 21994 in Peru Peru
Week rank: 66 Up1 in Peru Peru Up
Innovation award
Innovation award
Nominee: 6x

Winner: 1x



use Mateodioev\StringMatcher\{Config, Matcher};

__DIR__ . '/vendor/autoload.php';

$conf = new Config;
// match email's
$conf->addFormat('mail', "([a-z0-9!\#$%&'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!\#$%&'*+\\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9]))");
# $conf->addFormat('c', '([\d.]+)');

$matcher = new Matcher('/{name}?/{aaaa}/{mail:umail}?/', $conf);

$text = '/nombre/asds/';

var_dump($matcher->isValid($text, true));
$vars = $matcher->match($text, true);

json_encode($vars, JSON_PRETTY_PRINT);


String Vars

Get vars from a string


composer require mateodioev/string-vars

use Mateodioev\StringVars\{Config, Matcher};


$matcher = new Matcher(format: "Hello {name}");

// Validate input
var_dump($matcher->isValid('Hello Juan')); // true
var_dump($matcher->isValid('Hello _ Juan')); // false

$vars = $matcher->match('Hello Juan'); // $vars contain an array of parameters
var_dump($vars['name']); // "Juan"

Match with data types

// "w": all string
// "d": all numbers
// "f": all decimals
// "all": all characters
// "": all characters except /
$matcher = new Matcher(format: "Hello {w:name}");

Using custom formats

$conf = new Config();

// This match only decimals numbers
$conf->addFormat('c', '([\d]+\.[\d]+)');

$matcher = new Matcher('The price is {c:price}', $conf);

var_dump($matcher->isValid('The price is 33.03')); // true
var_dump($matcher->isValid('The price is 33')); // false

$vars = $matcher->match('The price is 33.03');
var_dump($vars['price']); // "33.03"

  Files folder image Files  
File Role Description
Files folder imagesrc (3 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Config.php Class Class source
  Plain text file Matcher.php Class Class source
  Plain text file StringMatcherException.php Class Class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:10,704
This week:116Up