It is unclear what information you want to obtain from URL, but follows below some alternatives.
Use this function to get the components that make up a URL, such as the Scheme, host, port, user, directory (after signal?
), etc..
Take an example:
$url = "https://docs.google.com/uc?id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download";
echo "<pre>" . print_r(parse_url($url));
// Resultado:
// Array (
// [scheme] => https
// [host] => docs.google.com
// [path] => /uc
// [query] => id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download
// )
To obtain only the host, do:
$url = "https://docs.google.com/uc?id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download";
$site = parse_url($url, PHP_URL_HOST);
echo $site . "\n"; // docs.google.com
See demonstração
The function substr
aims to return certain part of a string, it takes three arguments, the first is the string, the second is the initial position of the part you want to return, and the third indicates the amount of characters to be returned from the second argument.
Already the function strpos
finds the numerical position of the first occurrence of a needle in a haystack.
- Note: We will use the result of this function to use as the second argument in the function
substr
.
Code:
function extrairHostPorIndice($url){
// Extraí tudo que estiver depois das duas primeiras barras "//"
$depois = substr($url, strpos($url, "//") + strlen("//"));
// Extraí tudo que estiver antes da primeira barra "/"
$antes = substr($depois, 0, strpos($depois, "/"));
// Retorna o resultado
return $antes;
}
Example of use:
$url = "https://docs.google.com/uc?id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download";
$site = extrairHostPorIndice($url);
echo $site . "\n"; // docs.google.com
See demonstração
This function returns part of a string, the first argument is haystack, the second is the needle (reference point), the result will be the part that is after the reference point, if you pass the third argument as true
, the result will be the party that is before the reference point.
Code:
function extrairHostPorReferencia($url){
// Extraí tudo que estiver depois das duas primeiras barras "//
$depois = strstr($url, "//");
// Elimina os dois primeiros caracteres, que são "//"
$depois = substr($depois, 2) . "\n";
// Extraí tudo que estiver antes da primeira barra "/"
$antes = strstr($depois, '/', true);
return $antes;
}
Example of use:
$url = "https://docs.google.com/uc?id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download";
$site = extrairHostPorReferencia($url);
echo $site. "\n"; // docs.google.com
See demonstração
Note: If that’s not the information you want to get, say in the comments.