questo codice funziona e restituisce cio' che io volevo (senza regex)
https://3v4l.org/3facc#v5.6.0
Codice PHP:
<?php
function get_host($url) {
$host = parse_url($url, PHP_URL_HOST);
$names = explode(".", $host);
if(count($names) == 1) {
return $names[0];
}
$names = array_reverse($names);
return $names[1] . '.' . $names[0];
}
// Output for 5.6.0
echo get_host('http://www.site1.com/perma/doom-rock.htm'); // site1.com
echo "\n";
echo get_host('http://www.site2.net/mod/doom.html'); // site2.net
echo "\n";
echo get_host('https://www.site3.com/m/doom-rock'); // site3.com
echo "\n";
echo get_host('http://site4.com/doom'); // site4.com
echo "\n";
echo get_host('http://site5.com/doom'); // site5.com
Output for 5.6.0
site1.com
site2.net
site3.com
site4.com
site5.com
ho provato ad inserire lo switch case ma il risultato e' pessimo
sites.php
Codice PHP:
<?php
require_once "connetti.php";
$host = $row['links'];
function get_host($url) {
$host = parse_url($url, PHP_URL_HOST);
$names = explode(".", $host);
if(count($names) == 1) {
return $names[0];
}
$names = array_reverse($names);
return $names[1] . '.' . $names[0];
}
switch ($names)
{
case 'site1.com':
echo $names. ' ' . '<img src="http://forum.it.altervista.org/image_upload/sites/favicon1.ico"></img>' . ' ';
break;
case 'site2.net':
echo $names. ' ' . '<img src="http://forum.it.altervista.org/image_upload/sites/favicon2.ico"></img>' . ' ' ;
break;
case 'site3.com':
echo $names. ' ' . '<img src="http://forum.it.altervista.org/image_upload/sites/favicon3.ico"></img>' . ' ';
break;
case 'site4.com':
echo $names. ' ' . '<img src="http://forum.it.altervista.org/image_upload/favicon4.ico"></img>' . ' ' ;
break;
case 'site5.com':
echo $names. ' ' . '<img src="http://forum.it.altervista.org/image_upload/favicon5.ico"></img>' . ' ' ;
break;
default:
echo $names. ' ' . '(<span style="color:red;"> no flag</span>)' .'<img src="http://forum.it.altervista.org/image_upload/sites/nomatch.gif"</img>' . ' ' ;
}
//echo get_host
?>
la pagina che richiama la funzione
Codice PHP:
<?php
// include la funzione
require_once("sites.php");
echo get_host;
echo "\n";
?>