This is a function I wrote which has the intention of extracting bits of the URL.
For the latest version visit my GitHub.

function getPage($options = "", $page = "")
     *   - return the string untouched
     * a - return the page name, extension, and get variables
     * b - no extension, no get variable
     * c - url up to extension, no get variable
     * d - all get variables (everything after "?" excludes the "?") (if no "?" then sets to "")
     * e - domain name e.g. google
     * f - full domain without any page information e.g. http://www.google.com
     * g - full domain e.g. http://www.google.com/search?q=query (overwrites all other options)
    if (empty($options)) return $self;

    $options = str_split($options);
    $page = ($page !== "") ? $page : $self;

    if (in_array("a", $options)) $page = substr($page, strrpos($page, "/") + 1);
    if (in_array("b", $options)) $page = substr($page, 0, strrpos($page, ".php"));
    if (in_array("c", $options)) $page = substr($page, 0, strrpos($page, "?"));
    if (in_array("d", $options)) $page = (strpos($page, "?")) ? substr($page, strrpos($page, "?") + 1) : "";
    if (in_array("e", $options)) $page = (strpos($page, "www.")) ? substr($page, strpos($page, "www.") + 4, strpos($page, ".", strpos($page, "www.")) + 4) : ((strpos($page, "/localhost/")) ? substr($page, strpos($page, "://") + 3, strpos($page, "localhost/") + 2) : (strpos($page, "www") ? substr($page, strpos($page, "://") + 3, strlen($page) - strpos($page, "www.") + 5) : substr($page, strpos($page, "://") + 3, strpos($page, ".") - strpos($page, "://") - 3)));
    if (in_array("f", $options)) if (preg_match("/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/", $page, $matches)) $page = $matches[0];
    if (in_array("g", $options)) return $self;

    return $page;

I'm not sure if I am creating this in the best way (there are lots of substrings).

One of my current examples of using this code is to determine which page is selected.

<ul id="nav">
    <li class="button <?php echo getPage("ab") == "page1" ? "active" : "inactive"; ?>"><a href="page1.php">Page 1</a></li>
    <li class="button <?php echo getPage("ab") == "page2" ? "active" : "inactive"; ?>"><a href="page2.php">Page 2</a></li>

Assuming the URL is "http://www.example.com/page1.php?myVar=myVal"
getPage("ab") would find the page name.
a Would return the page name (e.g. "page1.php")
b Would return "http://www.example.com/page1"
Combining the two returns "page1".

  • 2
    \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$
    – Mast
    May 21, 2018 at 13:04
  • 1
    \$\begingroup\$ @Mast My apologies, I was not aware of the etiquette on this Exchange. \$\endgroup\$ May 21, 2018 at 14:17

1 Answer 1


Have you tried using this function before? It does a fair bit of what you are trying to accomplish.


Failing that, I would try learning regular expressions, as they can make a lot of your strpos more concise. Its a handy skill to have in the toolbox.


I also find the a,b,c thing incredibly cryptic You could use constants

const NO_EXT_NO_GET_VARS = 'b';
  • \$\begingroup\$ That function you'd linked is something I've not seen before. I create these functions just to expand my knowledge. I did try RegEx in one of the examples at the end. Regards to the letters, I agree, I'll change this. \$\endgroup\$ Apr 5, 2017 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.