I tested every solution you've been given (putting each into a separate function), and they all work perfectly on both the cnn site and the example you gave.
$strings[] = file_get_contents('http://www.cnn.com/');
$strings[] = '<title>Peet\\'s Coffee & Tea</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="keywords" content="coffee, specialty, gourmet coffee, coffee beans,
coffee blends, coffee gifts, tea gifts, tea, tea leaves and bags, herbal, green tea, black tea">';
foreach ($strings as $string) {
echo getMetaTagsDescription($string) . '<br />';
echo get_the_title($string) . '<br />';
echo get_the_title_and_keywords($string) . '<br />';
echo getTitle($string) . '<br />';
echo get_title($string) . '<br />';
}
function getMetaTagsDescription($html)
{
eregi('<META NAME="keywords" CONTENT="([^\\"]*)">', $html, $matches);
return $matches[1];
}
function get_the_title($html)
{
$matched = preg_match("/<title[^>]*>(.*?)<\/title>/is", $html, $match);
if($matched) {
return $match[1];
} else {
return "No title tag found";
}
}
function get_the_title_and_keywords($html)
{
$matched = preg_match("/<title[^>]*>(.*?)<\/title>/is", $html, $match);
if($matched) {
$title = $match[1];
} else {
$title = "No title tag found";
}
$matched = preg_match("/<meta name=.?keywords.? content=.?(.*?).?>/is", $html, $match);
if($matched) {
$keywords = $match[1];
} else {
$keywords = "No description";
}
return $title . '<br />' . $keywords;
}
function getTitle($html)
{
eregi("<title>(.*)</title>", $html, $matches);
return $matches[1];
}
function get_title($str)
{
if (($pos_1 = strpos($str, '<title>')) === false) {
return false;
}
if (($pos_2 = strpos($str, '</title>', $pos_1)) === false) {
return false;
}
return strip_tags(substr($str, $pos_1, $pos_2 - $pos_1));
}
/** Output:
CNN.com
CNN.com
No description
CNN.com
CNN.com
coffee, specialty, gourmet coffee, coffee beans, coffee blends, coffee gifts, tea gifts, tea, tea leaves and bags, herbal, green tea, black tea
Peet's Coffee & Tea
Peet's Coffee & Tea
coffee, specialty, gourmet coffee, coffee beans, coffee blends, coffee gifts, tea gifts, tea, tea leaves and bags, herbal, green tea, black tea
Peet's Coffee & Tea
Peet's Coffee & Tea
**/