Skip to content

Instantly share code, notes, and snippets.

@easychen
Last active December 25, 2015 15:49
Show Gist options
  • Select an option

  • Save easychen/7001126 to your computer and use it in GitHub Desktop.

Select an option

Save easychen/7001126 to your computer and use it in GitHub Desktop.

Revisions

  1. easychen revised this gist Oct 16, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions get_title_via_url.php
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@ function get_title_via_url( $url )
    if( preg_match( $reg , $content , $out ) )
    {
    $title = $out[1];
    if(!is_utf8( $content )) $title = mb_convert_encoding( $title , 'UTF-8' , 'GBK' );
    return $title;
    if(!is_utf8( $content )) $title = mb_convert_encoding( $title , 'UTF-8' , 'GBK' );
    return $title;
    }

    return basename( $url );
  2. easychen renamed this gist Oct 16, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. easychen revised this gist Oct 16, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion 抓取url中文标题
    Original file line number Diff line number Diff line change
    @@ -26,4 +26,5 @@ function is_utf8( $content )
    {
    return $out[1] == 'utf-8';
    }
    }
    }
    ?>
  4. easychen created this gist Oct 16, 2013.
    29 changes: 29 additions & 0 deletions 抓取url中文标题
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    <?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';
    }
    }