Ottenere il titolo di un sito con PHP!
Posted on 05. Nov, 2009 by daniele in php, tutorials
Questa bellissima funzione cattura il titolo dal tag title di un sito. E' possibile catturare il titolo della stessa pagina in cui si trova lo script, oppure potete specificare un URL a vostro piacimento.
function urltitle($url, $window = _blank) { // Start Function if ($url == null) { // Check if the URL is empty $url = ""; // Set function output to empty } else { // If not empty carry on $http = strpos($url, "http://"); // Check if the url has http:// in it if ($http === false) { // If not add it $url = "http://" . $url; // Add http:// to the url } $file = @fopen($url, "r"); // Open the url and read it if (!$file) { // If the url cant be opened $url = "<a href="$url" target="$window">$url</a>"; // If it cant be opened function output it the url with the url as the title } else { // If it can be opened carry on while (!feof($file)) { //Tests for end-of-file $line = fgets($file); // Reads file if (preg_match ("@\<title \>(.*)\</title>@i", $line, $out)) { { // Checks for title $url = "<a href="$url" target="$window">$out[1]</a>"; // echo the url with title break; } } } @fclose($file); // Close File } return $url; // Return function } //ESEMPIO APPLICATO echo urltitle("http://www.sastgroup.com", "_self"); // URL and Window Type echo "". urltitle("http://www.google.com"); // URL and Window Type


Leave a reply