Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl page title

Tags:

html

php

curl

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.

like image 854
Jake Avatar asked Dec 16 '22 15:12

Jake


1 Answers

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];
}
like image 56
Get Off My Lawn Avatar answered Jan 01 '23 16:01

Get Off My Lawn