PHP code example of mateodioev / string-vars

1. Go to this page and download the library: Download mateodioev/string-vars library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mateodioev / string-vars example snippets


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"

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


$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"