Skip to content

Instantly share code, notes, and snippets.

@easychen
Last active December 25, 2015 15:49
Show Gist options
  • Save easychen/7001126 to your computer and use it in GitHub Desktop.
Save easychen/7001126 to your computer and use it in GitHub Desktop.
做了下title转码、对于文件链接只取前1k(服务器支持断点下载的情况下
<?php
function get_title_via_url( $url )
{
// 抓取前1024个字节
$context=array('http' => array ('header'=> 'Range: bytes=0-1024' ));
$xcontext = stream_context_create($context);
$content = strtolower( file_get_contents( $url , FALSE,$xcontext ) );
$reg = '/<title>(.+?)<\/title>/is';
if( preg_match( $reg , $content , $out ) )
{
$title = $out[1];
if(!is_utf8( $content )) $title = mb_convert_encoding( $title , 'UTF-8' , 'GBK' );
return $title;
}
return basename( $url );
}
function is_utf8( $content )
{
$content = strtolower($content);
$reg = '/<meta.+?charset=[\'"]?([a-z0-9\-]+)[\'"]/is';
if(preg_match( $reg , $content , $out ))
{
return $out[1] == 'utf-8';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment