Get Title From Site
Posted by Pete | Posted in PHP Functions | Posted on 17-07-2009-05-2008
1
BioHosts staff have created a function to allow you to retrieve the site title of any website. This will allow you to include the title in links as we have shown. Keep reading for our fully commented function to do this yourself.
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 = "$url"; // 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 ("@\(.*)\ @i", $line, $out)) {
{ // Checks for title
$url = "$out[1]"; // echo the url with title
break;
}
}
}
@fclose($file); // Close File
}
return $url; // Return function
}
A demo on how to use this is below.
echo urltitle("http://www.pk-tuts.co.uk", "_self"); // URL and Window Type
echo "". urltitle("http://www.google.com"); // URL and Window Type



I don’t understand the preg_match();
.