Displaying link name through url

Asked

Viewed 217 times

3

I made this code below with everything not working the way I want, I need the code to do this: instead of presenting the position number in the variable $verificar, show the searched variable text $nomeServidor instead of the position, someone could help me with this?

In case there are some servers that I have verified that have in the case almost equal words taking some differentiated letters as the servers https://my.pcloud.com/ and https://cloud.com/, in case I’m having problems when performing the search with similar Urls because if in $nomeServidor has cloud so much research cloud as pcloud, how can I fix this?

function nomeServer ($servidorNome) {
    $urlservidor = $servidorNome;
    $nomeServidor   = 'google';
    $verificar = stripos($urlservidor, $nomeServidor);
    echo $verificar;
} 

echo nomeServer ('https://docs.google.com/uc?id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download');

I need the script take the name of URL using the reference placed in $nomeServidor exactly there if the result is right show value true otherwise value phony.

2 answers

3

It is unclear what information you want to obtain from URL, but follows below some alternatives.

Function parse_url()

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

Function substr() and strpos()

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

Function strstr()

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.

1


Use regular expression. In the case below you look exactly for the word in question. The function returns an Array() with the results.

$re = "/(\\W|^)google(\\W|$)/"; 
$str = "https://docs.google.com/uc?id=0B7zY19LCJa8XeXZtWklZbFBEVG8&export=download"; 

preg_match($re, $str, $matches);

print_r($matches);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.