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
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;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.