Lucas
Lucas

Reputation: 3715

Match URI by the asterisk

What I want to do is to automatically match the URI, and assign the correct description to them. Lets say I have the following URI's:

test
something/action
controller/test
controller/another
controller/action/something
action
action/test
controller/another/somethingelse

They are really random, but now I would like to use the following array (array, because I think it would be the best solution), to match them:

$config = array(
     'test' => 'The description of test',
     'something/action' => 'Some action description',
     'controller/another' => 'Another description',
     'controller/action/*' => 'This should match the `controller/action/blah` and everything similar like: `controller/action/something` and give this description'
     'action' => 'Action description',
     'action/*' => 'As the 2nd above, this should match the `action/test` itself, and similar like `action/anothertest` URI
);

This would be easy, in case of no asterisks... (simply $config[$uri] to match the description) but I would like to match the asterisks as well... So if there is a controller like controller/another/name and I would use the config like controller/another/* it would match the description for this array key.

Anyone have an idea on how could it be done? I need ideas basically, but any other answers are welcome!

Upvotes: 0

Views: 108

Answers (3)

Andreas Helgegren
Andreas Helgegren

Reputation: 1678

Suggested solution:

This will return all the descriptions that is comparable to the uri. There is room for improvement here. Cheers!

<?php

    $uris = array(
        'test',
        'something/action',
        'controller/test',
        'controller/another',
        'controller/action/something',
        'action',
        'action/test',
        'controller/another/somethingelse'
    );

    $sUri = $uris[4];
    print_r(
        getConfigBasedOnKey($sUri)
    );

    function getConfigBasedOnKey($sUri) {

        $config = array(
             'test' => 'The description of test',
             'something/action' => 'Some action description',
             'controller/another' => 'Another description',
             'controller/action/*' => 'This should match the controller/action/blah and everything similar like: controller/action/something and give this description',
             'action' => 'Action description',
             'action/*' => 'As the 2nd above, this should match the `action/test` itself, and similar like action/anothertest URI'
        );

        $descriptions = array();
        foreach($config as $configKey => $configDescription) {
            $configKey = preg_replace("/\*/",".*",$configKey);

            preg_match("|^$configKey$|",$sUri,$matches);
            if(count($matches) > 0) {
                $descriptions[] = $configDescription;
            }
        }

        return $descriptions;
    }

?>

Output:

Array
(
    [0] => This should match the controller/action/blah and everything similar like: controller/action/something and give this description
)

Upvotes: 1

Passerby
Passerby

Reputation: 10080

This can be done by combining fnmatch and something like array_filter:

$str=file_get_contents(/*... URIs ...*/);
$config = array(
     'test' => 'The description of test',
     'something/action' => 'Some action description',
     'controller/another' => 'Another description',
     'controller/action/*' => 'This should match the `controller/action/blah` and everything similar like: `controller/action/something` and give this description',
     'action' => 'Action description',
     'action/*' => 'As the 2nd above, this should match the `action/test` itself, and similar like `action/anothertest` URI'
);
foreach(explode("\n",$str) as $line)
{
    $line=trim($line);
    $dkey=array_filter(array_keys($config),function($key)use($line){return fnmatch($key,$line);});
    if(count($dkey)<=0)
    {
        echo "(no description)";
    }
    else
    {
        echo $config[reset($dkey)];
    }
    echo "\n";
}

Online demo

The above code outputs:

The description of test
Some action description
(no description)
Another description
This should match the `controller/action/blah` and everything similar like: `controller/action/something` and give this description
Action description
As the 2nd above, this should match the `action/test` itself, and similar like `action/anothertest` URI
(no description)

The above code can be improved: you can cache the array_keys result outside loop so it don't need to be called over and over again.

Upvotes: 1

stema
stema

Reputation: 93026

what you want is .*. . is in regex the character class, that matches any character (by default except newlines) and then you need the quantifier * to say match any character 0 or more times.

So your regex would look like

controller/action/.*

to match

controller/action/
controller/action/blah
controller/action/something
...

Upvotes: 1

Related Questions