PHPでhtmlページのタイトルタグ(任意のタグの内容でも可能)を取得するには。
function _get_element( $pElement, $pSource ) {
$_data = null;
$pElement = strtolower( $pElement );
$_start = strpos( strtolower( $pSource ), chr(60) . $pElement, 0 );
$_start = strpos( $pSource, chr(62), $_start ) + 1;
$_stop = strpos( strtolower( $pSource ), "</" . $pElement . chr(62), $_start );
if( $_start > strlen( $pElement ) && $_stop > $_start ) {
$_data = trim( substr( $pSource, $_start, $_stop - $_start ) );
}
return( $_data );
}
function _get_page_title( $url ) {
$html = file_get_contents($url);
$html = mb_convert_encoding( $html, 'UTF-8', 'AUTO' );
$title = $this->_get_element( 'title', $html );
$html = mb_convert_encoding($html, mb_internal_encoding(), "auto" );
if ( $title ) {
return $title;
}
else {
return 'non-title';
}
}
として
$url = 'http://www.yahoo.co.jp/'; $title = $this->_get_page_title( $url );
とします。
http://d.hatena.ne.jp/steel-plate/20080417/1208445174