PHP code example of weew / url-matcher

1. Go to this page and download the library: Download weew/url-matcher 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/ */

    

weew / url-matcher example snippets


$matcher = new UrlMatcher();

// true
$matcher->match('users/{id}', 'users/1');

// false
$matcher->match('users/{id}', 'users');

// true
$matcher->match('users/{id?}', 'users/1');

// true
$matcher->match('users/{id?}', 'users');

$matcher->addPattern('id', '[0-9]+');

// true
$matcher->match('users/{id}', 'users/1');

// false
$matcher->match('users/{id}', 'users/abc');

// true
$matcher->match('users/{id}', 'users/1', [
    'id' => '[0-9]+',
]);

// true
$matcher->match('users/{id?}', 'users/1', [
    'id' => '[0-9]+',
]);

// true
$matcher->match('users/{id?}', 'users', [
    'id' => '[0-9]+',
]);

$dictionary = $matcher->parse('users/{id}', 'users/123');
// 123
$dictionary->get('id');

$dictionary = $matcher->parse('users/{id}', 'users');
// null
$dictionary->get('id');

$matcher->addPattern('id', '[0-9]+');
$dictionary = $matcher->parse('users/{id}', 'users/123');
// 123
$dictionary->get('id');

$dictionary = $matcher->parse('users/{id}', 'users/abc');
// null
$dictionary->get('id');

// api.service.com
$matcher->replace('{subdomain}.service.com', 'subdomain', 'api');

// api.service.com/v1
$matcher->replaceAll('{subdomain}.service.com/{version}', ['subdomain' => 'api', 'version' => 'v1']);