Mobile Detect

モバイル判定などデバイスの情報から判別するライブラリ。
PHPで、ユーザーエージェントを取得するときは「$_SERVER[‘HTTP_USER_AGENT’]」で取得するが、便利なライブラリがある、
Mobile Detect は、モバイル デバイス (タブレットを含む) を検出するための軽量の PHP クラスです。User-Agent 文字列を特定の HTTP ヘッダーと組み合わせて使用し、モバイル環境を検出します。

インストール

– 最新リリースをダウンロード
– Mobile_Detect.php
Gitからダウンロードして、その中のMobile_Detect.phpだけを使う。
require_once “libs/Mobile_Detect.php” ;

composer パッケージとしてインストールする
composer require mobiledetect / mobiledetectlib
またはcomposer.jsonファイルに依存関係を含める。
{
 ”require”: {
  ”mobiledetect/mobiledetectlib”: “^2.8”
 }
}

Develop

// クラスファイルを読み込みインスタンス化、パスは環境に応じて変更

include ( ‘./MobileDetect.php’ );
$detect = new \Detection\MobileDetect;

// 任意のモバイル デバイス (スマホまたはタブレット)。
if ( $detect->isMobile() ) {
  // デバイスがスマホまたはタブレットの場合に実行する処理
  echo “デバイスはスマホまたはタブレットです。”;
}

// 任意のタブレット デバイス。
if( $detect->isTablet() ){
  // デバイスがタブレットの場合に以下を表示
  echo “デバイスはタブレットです。”;
}

// タブレットを除外する、スマホのみ
if( $detect->isMobile() && !$detect->isTablet() ){
  // デバイスがスマホの場合に以下を表示
  echo “デバイスはスマホです。”;
}

// $detect->isMobile() が true でない場合、モバイルで無い、デバイスがPCの場合
if ( !$detect->isMobile() ) {
  // デバイスが PC の場合に以下を表示
  echo “デバイスは PC です。”;
}

// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){

}

if( $detect->isAndroidOS() ){

}

// Alternative method is() for checking specific properties.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->is(‘Chrome’)
$detect->is(‘iOS’)
$detect->is(‘UC Browser’)
// […]

// Batch mode using setUserAgent():
$userAgents = array(
 ’Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19′,
‘BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103’,
// […]
);
foreach($userAgents as $userAgent){
  $detect->setUserAgent($userAgent);
  $isMobile = $detect->isMobile();
  $isTablet = $detect->isTablet();
  // Use the force however you want.
}

// Get the version() of components.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->version(‘iPad’); // 4.3 (float)
$detect->version(‘iPhone’) // 3.1 (float)
$detect->version(‘Android’); // 2.1 (float)
$detect->version(‘Opera Mini’); // 5.0 (float)
// […]

Laravelでは
– Agent は、 Mobile Detect に基づく Laravel のユーザー エージェント クラスで、いくつかの追加機能があります。イェンス・シーガース
– Laravel Mobile Detect は、 Laravel Blade テンプレートでデバイス検出を使用できるようにするパッケージです。(よく知られている、常に更新されている PHP モバイル検出ライブラリを利用します。) Barnabas Kecskes
– BrowserDetect は、ブラウザーおよびモバイル検出パッケージであり、Laravel に最適なユーザー エージェント識別子を収集してラップします。Varga Zsolt

Previous article

Line Login

Next article

OpenAI