Skip to main content
Added link to GitHub
Source Link

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

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

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

Rollback to Revision 1
Source Link
Mast
  • 13.5k
  • 12
  • 54
  • 119

PHP function Function to extract parts ofget 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["REQUEST_SCHEME"]}://{$_SERVER["SERVER_NAME"]$_SERVER["HTTP_HOST"]}{$_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. httpshttp://www.google.com
     * g - urlfull updomain toe.g. lasthttp://www.google.com/search?q=query folder,(overwrites excludesall pageother nameoptions)
     */
    if (empty($options)) return $self;
    
    $options = str_split($options);
    $page = ($page !== "") ? $self$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 = 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('strpos($page, "www.'")) ? substr($page, parse_urlstrpos(getPage$page, "www.") + 4, strpos('f'$page, ".", strpos($page), PHP_URL_HOST"www."))[1] ??+ 4) : (parse_url(getPagestrpos('f', $page), PHP_URL_HOST"/localhost/")) ?? 'localhost');
substr($page, strpos($page, "://") + if3, (in_arraystrpos("f"$page, $options)"localhost/") $page+ =2) : (strpos($page ==, $self"www") ? "{$scheme}://{$_SERVER["SERVER_NAME"]}"substr($page, :strpos($page, "{$scheme}://".) + 3, strlen($page) - strpos($page, $scheme"www.") ===+ false5) ?: parse_urlsubstr($page, strpos($page, "{$scheme}://{") + 3, strpos($page}, ".")["host"] :- parse_urlstrpos($page, "://")["host"] - 3)));
    if (in_array("g""f", $options)) $page =if substr($pagepreg_match("/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/", 0$page, strrpos($matches)) $page = $matches[0];
    if (in_array("g", "/"$options)); return $self;

    return $page;
}

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();

PHP function to extract parts of 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;
}

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();

Function to get URL

function getPage($options = "", $page = "")
{
    $self = "{$_SERVER["REQUEST_SCHEME"]}://{$_SERVER["HTTP_HOST"]}{$_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. 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;
}
Edited the title and added another example of how to use the function; oh, and I edited the function!
Source Link

Function PHP function to getextract parts of 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 = "{$_SERVER["REQUEST_SCHEME"]$scheme}://{$_SERVER["HTTP_HOST"]$_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. httphttps://www.google.com
     * g - fullurl domainup e.g.to http://www.google.com/search?q=querylast (overwritesfolder, allexcludes otherpage options)name
     */
    if (empty($options)) return $self;
    
    $options = str_split($options);
    $page = ($page !== "") ? $page$self : $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 = (strposexplode($page, "www'.")) ? substr($page', strposparse_url($page, "www.") + 4, strposgetPage($page, "."'f', strpos($page), "www.")PHP_URL_HOST) + 4)[1] :?? (parse_url(strposgetPage($page'f', "/localhost/"$page), PHP_URL_HOST) ?? substr($page,'localhost');
 strpos($page, "://") + 3,if strpos($pagein_array("f", "localhost/"$options) + 2) :$page (strpos= ($page, "www"== $self) ? substr($page, strpos($page, "{$scheme}://") + 3, strlen($page) - strpos($page, "www.{$_SERVER["SERVER_NAME"]}") + 5) : substr($page, strpos($page, "{$scheme}://") + 3, .(strpos($page, "."$scheme) -=== strpos($page,false ? parse_url("{$scheme}://{$page}")["host"] -: 3)parse_url($page)["host"]);
    if (in_array("f""g", $options)) if (preg_match("/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/", $page, $matches)) $page = $matches[0];
   substr($page, if0, (in_arraystrrpos("g"$page, $options"/")) return $self;;

    return $page;
}

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();

Function to get URL

function getPage($options = "", $page = "")
{
    $self = "{$_SERVER["REQUEST_SCHEME"]}://{$_SERVER["HTTP_HOST"]}{$_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. 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;
}

PHP function to extract parts of 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;
}

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();
Source Link
Loading