Jump to content

Every possible match


iSE

Recommended Posts

Hi,

 

I'm interested in sending the REQUEST_URI variable to a MySQL database, and retrieving the data for that particular page. The problem is that since all the URL's are dynamically changing, only part of the REQUEST_URI may be specified in the database.

 

So for instance, the REQUEST_URI may be:

/holdcroft-renault-hanley/used-cars/2/4/price-asc/

 

And the request_uri fields in the database will be something like:

/holdcroft-renault-hanley/used-cars/

/new-cars/

/used-cars/

/holdcroft-renault-hanley/

/price-asc/

 

What I need to do, is to create a regex pattern so that preg_match_all will be able to get every possible combination of /(.*)/. I've been trying various different methods and patterns only to keep on getting stuck.

 

So far I have:

preg_match_all('/\/?(.*)\/?/', $_SERVER['REQUEST_URI'], $matchesArray);
var_dump($matchesArray);

 

and this gives me:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(50) "/holdcroft-renault-hanley/used-cars/2/4/price-asc/"
    [1]=>
    string(0) ""
  }
  [1]=>
  array(2) {
    [0]=>
    string(49) "holdcroft-renault-hanley/used-cars/2/4/price-asc/"
    [1]=>
    string(0) ""
  }
}

 

What I want to get, is an array filled with every possibility, so:

/holdcroft-renault-hanley/used-cars/2/4/price-asc/
/holdcroft-renault-hanley/used-cars/2/4/
/holdcroft-renault-hanley/used-cars/2/
/holdcroft-renault-hanley/used-cars/
/holdcroft-renault-hanley/
/used-cars/2/4/price-asc/
/used-cars/2/4/
/used-cars/2/
/used-cars/
/2/4/price-asc/
/2/4/
/2/
/4/price-asc/
/4/
/price-asc/

 

are all the possible matches, how do I create a regular expression to collect these results? :confused:

 

As always, all help is very much appreciated, and indeed passed on.

 

Regards,

 

iSE

Link to comment
https://forums.phpfreaks.com/topic/211796-every-possible-match/
Share on other sites

ttry

<?php
$test = '/holdcroft-renault-hanley/used-cars/2/4/price-asc/';
$a = perm($test);
print_r($a);

function perm($a){
    $out = array();
    $a = explode('/', trim($a, '/'));
    $a1 = $a;
    if ($a[0] ){
        while (count($a)){
            $out[] = '/'. implode('/', $a).'/';
            array_pop($a);
        }
        array_shift($a1);
        $c = perm(implode('/', $a1));
        $out = array_merge($out, $c);
    }
    return $out;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.