It's not clear what information you want to get from URL , but here are some alternatives below.
Use this function to get the components that make up a URL, such as scheme , host , port, user, directory (after the sign of ?
), etc.
See 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 get 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
View demonstração
The function substr
aims to return a certain part of a string , it receives three arguments, the first is the string , the second is the initial part that you want to return, and the third one indicates how many characters to return from the second argument.
The strpos
function 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 usage:
$url = "https://docs.google.com/uc?id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download";
$site = extrairHostPorIndice($url);
echo $site . "\n"; // docs.google.com
View demonstração
This function returns part of a string , the first argument is the haystack , the second is the needle 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 part 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 usage:
$url = "https://docs.google.com/uc?id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download";
$site = extrairHostPorReferencia($url);
echo $site. "\n"; // docs.google.com
View demonstração
Note : If this is not the information you want to get, say it in the comments.