Jake
Jake

Reputation: 3486

curl page title

I am using the following code to get the full html from a specified page:

$url = "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);

Question: how can this code be modified to return the <title> instead of the full html of the page. $result stores the result.

Upvotes: 4

Views: 9817

Answers (3)

Tim Joyce
Tim Joyce

Reputation: 4517

You can't really just get the title, you can get the whole document and then weed out the elements you need: I like to use Simple Html Dom Parser

$html = file_get_html('http://www.google.com/');
$title = $html->find('title');

Upvotes: 4

Get Off My Lawn
Get Off My Lawn

Reputation: 36311

You can get the title using regular expression, I find this regex very helpful:

function get_html_title($html){
    preg_match("/\<title.*\>(.*)\<\/title\>/isU", $html, $matches);
    return $matches[1];
}

Upvotes: 11

Shaun Hare
Shaun Hare

Reputation: 3871

Look at parsing the contents of the result

either using regex

or Dom Document

Upvotes: -3

Related Questions