Creating a single function

Asked

Viewed 41 times

3

How can I make the code below stand in a single function, 'cause I’m having a little trouble.

<?php

// Procura por um valor especifico para aplicar valor verdadeiro ou falso
function encontrarValor($urlgeral, $nomeserver){
    if (preg_match("/(\\W|^){$nomeserver}(\\W|$)/i", $urlgeral))
        return true;
    else
        return false;
}


$host = 'paste2.com = Mega,meohost.com = Meo,google.com = gdriver';

$separacao = explode(',',$host);
foreach ($separacao as $i) {
$filtro = explode(' = ',$i);

// Aplica o valor da url e verifica se a palavra e a mesma caso seja ira por o valhor = para trocar.

if (encontrarValor('google.com', $filtro[0])) { 
    echo "sim ". $filtro[0]." = ".$filtro[1]." \n"; 
}  else { 
    echo "$filtro[1] não e o servidor cadastrado. \n"; 
}


}

?>

1 answer

3


From what I understand you want to check if any item on the list matches google.com:

$hosts = 'paste2.com = Mega,meohost.com = Meo,google.com = gdriver';
foreach(explode(',', $hosts) as $item) {
    list($url, $nome) = explode(' = ', $item);
    $lista[$url] = $nome;
}

if (array_key_exists($link = 'google.com', $lista)) {
    echo "sim $link=$lista[$link]";
}
  • Not only google any server in the case in the function only was the google value I made true.

Browser other questions tagged

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