Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dinizgb/242087f66f2d859effffadbaf22fe00b to your computer and use it in GitHub Desktop.
Save dinizgb/242087f66f2d859effffadbaf22fe00b to your computer and use it in GitHub Desktop.
get-url-parts-in-php
<?php
/*
Script: Get URL Parts in PHP
Author: Gabriel Diniz
*/
$url = 'https://www.example.com/pathname?queryname=googley';
//FULL PARSING
var_dump(parse_url($url));
echo "<hr>";
//PARTIAL PARSING
var_dump(parse_url($url, PHP_URL_SCHEME));
var_dump(parse_url($url, PHP_URL_HOST));
var_dump(parse_url($url, PHP_URL_PATH));
var_dump(parse_url($url, PHP_URL_QUERY));
echo "<hr>";
//PARTS TO ECHO
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query = parse_url($url, PHP_URL_QUERY);
echo "HOST: $host";
echo "<br>";
echo "PATH: $path";
echo "<br>";
echo "QUERY: $query";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment