Showing link name through url

3

I did this code with everything not working the way I want it, I need the code to do the following: instead of displaying the position number in the variable $verificar , show the searched text of the variable $nomeServidor instead of the position, could anyone help me with this?

In the case there are some servers that I have verified that have in the case almost equal words taking some differentiated letters like the servers https://my.pcloud.com/ and https://cloud.com/ , in case I am having problems when searching with similar URLs because if in $nomeServidor has cloud search both cloud and 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 to get the name of the URL using the reference placed in $nomeServidor exactly in case the result is correct show / em> false otherwise false .

    
asked by anonymous 02.06.2015 / 02:30

2 answers

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);
    
02.06.2015 / 13:50
3

It's not clear what information you want to get from URL , but here are some alternatives below.

Function parse_url()

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

Functions substr() and strpos()

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

Function strstr()

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.

    
02.06.2015 / 15:02