0

Following regex works fine for me for most of url. But in case of few url, it does not give title, though source code has title.

$data = file_get_contents($url);
$title = get_title($data);
echo $title;
function get_title($html) 
    {
        return preg_match('!<title>(.*?)</title>!i', $html, $matches) ? $matches[1] : '';
    }

Here is the demo : DEMO

user123
  • 5,269
  • 16
  • 73
  • 121
  • i think that this question has been answered before here in SOF, check this http://stackoverflow.com/questions/13510124/regular-expression-to-get-page-title – Ma'moon Al-Akash Nov 26 '13 at 06:03
  • just change your regex to the one mentioned in my answer – Nishant Nov 26 '13 at 06:12
  • From your demo, it looks like the issue isn't with your regex, but is with getting the page contents in the first place via `file_get_contents()`. – ajp15243 Nov 26 '13 at 06:12

2 Answers2

1

As a solution to your problem please try executing following code snippet

<?php
 $url= 'http://www.indianic.com';
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $result=curl_exec($ch);
 curl_close($ch); 
 //echo $result;  
$title = get_title($result);
 echo $title;
function get_title($html) 
{
            return preg_match('!<title>(.*?)</title>!i', $html, $matches) ? $matches[1] : '';
}
?>
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26
1

Try this one,

 return preg_match('/<title[^>]*>(.*?)<\\/title>/ims', $html, $matches) ? $matches[1] : '';

Checked and working

$url='http://www.ndtv.com/';
    $data = file_get_contents($url);
    $title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $data, $matches) ? $matches[1] : '';
    echo $title;

OUTPUT :- NDTV.com: India, Business, Bollywood, Cricket, Video and Breaking News

Nishant
  • 3,614
  • 1
  • 20
  • 26
  • 1
    Reason for that is file_get_contents() not working for facebook, try this one http://stackoverflow.com/questions/7437688/loading-fb-thru-php-file-get-contents-throws-you-are-using-an-incompatible-we – Nishant Nov 26 '13 at 06:30