Skip to content

Instantly share code, notes, and snippets.

@kisexu

kisexu/getTitle Secret

Last active September 4, 2016 21:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kisexu/b64bc6ab787f302ae838 to your computer and use it in GitHub Desktop.
Save kisexu/b64bc6ab787f302ae838 to your computer and use it in GitHub Desktop.
Get title of website via link and convert title to utf-8 character encoding. 获取网站标题,并转换编码至utf-8.
<?php
function getTitle($url)
{
// get html via url
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($ch);
curl_close($ch);
// get title
preg_match('/(?<=<title>).+(?=<\/title>)/iU', $html, $match);
$title = empty($match[0]) ? 'Untitled' : $match[0];
$title = trim($title);
// convert title to utf-8 character encoding
if ($title != 'Untitled') {
preg_match('/(?<=charset\=).+(?=\")/iU', $html, $match);
if (!empty($match[0])) {
$charset = str_replace('"', '', $match[0]);
$charset = str_replace("'", '', $charset);
$charset = strtolower( trim($charset) );
if ($charset != 'utf-8') {
$title = iconv($charset, 'utf-8', $title);
}
}
}
return $title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment