Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7129817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:17:00+00:00 2026-05-28T11:17:00+00:00

Just writing a little function here and need some optimisation help! All requests redirect

  • 0

Just writing a little function here and need some optimisation help!

All requests redirect to the index page,

I have this function that parses a url into an array.

The type of url is depicted as:

http://localhost/{user}/{page}/?sub_page={sub_page}&action={action}

So an example would be:

http://localhost/admin/stock/?sub_page=products&action=add

When requesting the uri the domain is excluded so my function accepts strings like so:

/admin/stock/?sub_page=products&action=add

My function is as follows and WARNING it’s very procedural.

for those of you that cannot be bothered to read and understand it, ive added an explaination at the bottom 😉

function uri_to_array($uri){
    // uri will be in format: /{user}/{page}/?sub_page={subpage}&action={action} ... && plus additional parameters

    // define array that will be returned
    $return_uri_array = array();

    // separate path from querystring;
    $array_tmp_uri = explode("?", $uri);

    // if explode returns the same as input $string, no delimeter was found
    if ($uri == $array_tmp_uri[0]){ 

        // no question mark found.
        // format either '/{user}/{page}/' or '/{user}/'
        $uri = trim($array_tmp_uri[0], "/");

        // remove excess baggage
        unset ($array_tmp_uri);

        // format either '{user}/{page}' or '{user}'
        $array_uri = explode("/", $uri);

        // if explode returns the same as input $string, no delimiter was found
        if ($uri == $array_uri[0]){
            // no {page} defined, just user.
            $return_uri_array["user"] = $array_uri[0];
        }
        else{
            // {user} and {page} defined.
            $return_uri_array["user"] = $array_uri[0];
            $return_uri_array["page"] = $array_uri[1];            
        }
    }
    else{

        // query string is defined
        // format either '/{user}/{page}/' or '/{user}/'
        $uri = trim($array_tmp_uri[0], "/");
        $parameters = trim($array_tmp_uri[1]);

        // PARSE PATH
        // remove excess baggage
        unset ($array_tmp_uri);

        // format either '{user}/{page}' or '{user}'
        $array_uri = explode("/", $uri);

        // if explode returns the same as input $string, no delimiter was found
        if ($uri == $array_uri[0]){
            // no {page} defined, just user.
            $return_uri_array["user"] = $array_uri[0];
        }
        else{
            // {user} and {page} defined.
            $return_uri_array["user"] = $array_uri[0];
            $return_uri_array["page"] = $array_uri[1];            
        }

        // parse parameter string
        $parameter_array = array();
        parse_str($parameters, $parameter_array);

        // copy parameter array into return array
        foreach ($parameter_array as $key => $value){
            $return_uri_array[$key] = $value;
        }
    }
    return $return_uri_array;
}

basically there is one main if statement, one path is if no querystring is defined (no ‘?’) and the other path is if the ‘?’ does exist.

I’m just looking to make this function better.

Would it be worth making it a class?

Essentially i need a function that takes /{user}/{page}/?sub_page={sub_page}&action={action} as an argument and returns

array(
    "user" => {user},
    "page" => {page},
    "sub_page" => {sub_page},
    "action" => {action}
)

Cheers, Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:17:00+00:00Added an answer on May 28, 2026 at 11:17 am

    If you want to

    • Do it properly
    • Use a regular expression
    • Use the same method to parse all URL:s (parse_url() does not support relative paths, called only_path below)

    This might suite your taste:

    $url = 'http://localhost/admin/stock/?sub_page=products&action=add';
    preg_match ("!^((?P<scheme>[a-zA-Z][a-zA-Z\d+-.]*):)?(((//(((?P<credentials>([a-zA-Z\d\-._~\!$&'()*+,;=%]*)(:([a-zA-Z\d\-._~\!$&'()*+,;=:%]*))?)@)?(?P<host>([\w\d-.%]+)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(\[([a-fA-F\d.:]+)\]))?(:(?P<port>\d*))?))(?<path>(/[a-zA-Z\d\-._~\!$&'()*+,;=:@%]*)*))|(?P<only_path>(/(([a-zA-Z\d\-._~\!$&'()*+,;=:@%]+(/[a-zA-Z\d\-._~\!$&'()*+,;=:@%]*)*))?)|([a-zA-Z\d\-._~\!$&'()*+,;=:@%]+(/[a-zA-Z\d\-._~\!$&'()*+,;=:@%]*)*)))?(?P<query>\?([a-zA-Z\d\-._~\!$&'()*+,;=:@%/?]*))?(?P<fragment>#([a-zA-Z\d\-._~\!$&'()*+,;=:@%/?]*))?$!u", $url, $matches);
    $parts = array_intersect_key ($matches, array ('scheme' => '', 'credentials' => '', 'host' => '', 'port' => '', 'path' => '', 'query' => '', 'fragment' => '', 'only_path' => '', ));
    var_dump ($parts);
    

    It should cover just about all possible well-formed URL:s

    If host is empty, only_path should hold the path, that is protocol-less and host-less URL.

    UPDATE:

    Maybe I should read the question a bit better. This will parse the URL into components that you can use to more easily get the parts you’re really interested in. Run something like:

    // split the URL
    preg_match ('!^((?P<scheme>[a-zA-Z][a-zA-Z\d+-.]*):)?(((//(((?P<credentials>([a-zA-Z\d\-._~\!$&'()*+,;=%]*)(:([a-zA-Z\d\-._~\!$&'()*+,;=:%]*))?)@)?(?P<host>([\w\d-.%]+)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(\[([a-fA-F\d.:]+)\]))?(:(?P<port>\d*))?))(?<path>(/[a-zA-Z\d\-._~\!$&'()*+,;=:@%]*)*))|(?P<only_path>(/(([a-zA-Z\d\-._~\!$&'()*+,;=:@%]+(/[a-zA-Z\d\-._~\!$&'()*+,;=:@%]*)*))?)|([a-zA-Z\d\-._~\!$&'()*+,;=:@%]+(/[a-zA-Z\d\-._~\!$&'()*+,;=:@%]*)*)))?(\?(?P<query>([a-zA-Z\d\-._~\!$&'()*+,;=:@%/?]*)))?(#(?P<fragment>([a-zA-Z\d\-._~\!$&'()*+,;=:@%/?]*)))?$!u', $url, $matches);
    $parts = array_intersect_key ($matches, array ('scheme' => '', 'credentials' => '', 'host' => '', 'port' => '', 'path' => '', 'query' => '', 'fragment' => '', 'only_path' => '', ));
    
    // extract the user and page
    preg_match ('!/*(?P<user>.*)/(?P<page>.*)/!u', $parts['path'], $matches);
    $user_and_page = array_intersect_key ($matches, array ('user' => '', 'page' => '', ));
    
    // the query string stuff
    $query = array ();
    parse_str ($parts['query'], $query);
    

    References:

    Just to clarify, here are the relevant documents used to formulate the regular expression:

    1. RFC3986 scheme/protocol
    2. RFC3986 user and password
    3. RFC1035 hostname
      • Or RFC3986 IPv4
      • Or RFC2732 IPv6
    4. RFC3986 query
    5. RFC3986 fragment
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with a program that I am writing for my Systems
I was just writing some quick code and noticed this complier error Using the
I have just started writing my own JavaScript Framework (just for the learning experience),
I'm writing a small web that just makes some animation and shows some information
EDIT: I just found my problem after writing this long post explaining every little
I'm writing my first rails plugin and could use a little help. In a
I'm writing a little python script to help me automate the creation of mysql
I wrote this little function for writing out HTML tags: def html_tag(tag, content=None, close=True,
i'm writing a little c++ app to wrap around the opencv haar training function
So, here's a funny little programming challenge. I was writing a quick method to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.