Skip to main content
2 of 4
Edited the title and added another example of how to use the function; oh, and I edited the function!

PHP function to extract parts of URL

This is a function I wrote which has the intention of extracting bits of the URL.

function getPage($options = "", $page = "")
{
    preg_match("/^https?/", $page, $scheme);
    $scheme = $scheme[0] ?? null;
    $scheme = !empty($scheme) ? $scheme : (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https" : "http");
    $self = "{$scheme}://{$_SERVER["SERVER_NAME"]}{$_SERVER["REQUEST_URI"]}";
    /*
     *   - 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. https://www.google.com
     * g - url up to last folder, excludes page name
     */
    if (empty($options)) return $self;
    
    $options = str_split($options);
    $page = ($page == "") ? $self : $page;

    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 = empty(substr($page, 0, strrpos($page, "?"))) ? $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 = explode('.', parse_url(getPage('f', $page), PHP_URL_HOST))[1] ?? (parse_url(getPage('f', $page), PHP_URL_HOST) ?? 'localhost');
    if (in_array("f", $options)) $page = ($page == $self) ? "{$scheme}://{$_SERVER["SERVER_NAME"]}" : "{$scheme}://".(strpos($page, $scheme) === false ? parse_url("{$scheme}://{$page}")["host"] : parse_url($page)["host"]);
    if (in_array("g", $options)) $page = substr($page, 0, strrpos($page, "/"));

    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>
</ul>

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".

Another example is to check if a link is external using getPage('e') to return just the domain name.
A coded example being:

// Wrap in a div since XML must have one overall parent
$xml = simplexml_load_string('<div>'.$blog->story.'</div>');

// Iterate over all paragraph elements
foreach ($xml->children() as $p) {
    // Get external links
    // Here we specify the href of the anchor and see if the domain of the
    // anchor is the same as the domain of the current page we are on, we
    // will progress if the anchor points to an external site
    if (isset($p->a['href']) && getPage('e', $p->a['href']) != getPage('e')) {
        // Add the target attribute
        $p->a->addAttribute('target', '_blank');
    }
}

// Echo the HTML string
echo $xml->asXML();