LoginSignup
0
0

More than 3 years have passed since last update.

【PHP】Google検索結果URLをサクっと生成する

Last updated at Posted at 2019-06-07

コード

$queryAry = [
    'q' => 'サンプル',     // 検索ワード
    'ie' => 'utf-8',       // 検索ワードの文字コード(念の為指定)
    'newwindow' => '1',    // 検索結果へ飛ぶ際に新しいウインドウを開く
    'tbs' => 'qdr:y2',     // 期間指定(とりあえず2年)
];

//-- 指定しないパラメータは削除
$queryAry = array_filter($queryAry, 'strlen');

//-- 生成したURLを出力
echo 'https://www.google.co.jp/search?', http_build_query($queryAry, '', '&');

実行結果


Qiita 等に Google の検索結果 URL を載せる際、少しでも面倒だなと思ったら、数十秒でスッキリ&サクッと書けるのが、PHP の良いところですね。

※今回のようなコードは、ウェブブラウザから実行結果を見るのではなく、CLI 版の PHP から実行結果を見る事を想定しています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0