PHPでサイトのタイトル、キーワード、description文の取得 | SEO・アフィリ・PHPに関するブログ

PHPでサイトのタイトル、キーワード、description文の取得

    //=-=-=-=-=-=-=-=-=-
    //=-タイトルの取得-=
    //=-=-=-=-=-=-=-=-=-
    $html = file_get_contents($url);
    mb_language("Japanese");
    $html = mb_convert_encoding($html, "SJIS", "auto" );
    preg_match( "/<title>(.*?)<\/title>/i", $html, $matches);
    $info['title'] =  $matches[1];

   
    //━━━━━━━━━
    //┃メタタグ情報取得┃
    //━━━━━━━━━
    $meta = get_meta_tags($url);
   
    //=-=-=-=-=-=--=
    //=-キーワード-=
    //=-=-=-=-=-=--=
    $keywords = $meta['keywords'];
    $keywords = mb_convert_encoding($keywords, "SJIS", "auto" );
    //キーワードを切り分けて配列にセット
    $info['keywords']    = explode(",", $keywords);
    //キーワード前後の空白削除
    for ($i = 0; $i < count($info['keywords']); $i++) {
        $info['keywords'][$i]    = trim(mb_convert_kana($info['keywords'][$i], "s"));
    }


    //=-=-=-=-=-=--=-=-
    //=-description文-=
    //=-=-=-=-=-=--=-=-
    $info['description'] = $meta['description'];
    $info['description'] = mb_convert_encoding($info['description'], "SJIS", "auto" );
   


実行PHPファイルがUTF-8の場合は赤文字のSJISをUTF-8に変更
キーワードは二次配列になっているので注意