如何根据操作系统更改Web内容

时间:2016-11-25 04:18:00

标签: php html wordpress

我正在为移动应用设计一个wordpress网站。 对于访问者,我想显示基于操作系统的文本,图像,如下页 -

https://www.sapphireone.com/accounting-software/accounts/
在此页面中,在“帐户结构”部分中'有一个图像会根据操作系统而变化,即mac和windows。

我想为Android和iPhone做类似的事情。我使用以下代码,但它无法正常工作。

// function.php的代码

function find_andoird() {
$ua = $_SERVER[‘HTTPS_USER_AGENT’];

 /*    ====    Detect the OS    ====    */

 // Android
    $android        = strpos($ua, 'Android') ? true : false;

 // iPhone
   $iphone        = strpos($ua, 'iPhone') ? true : false;

  return $android;

 }

// 模板文件的代码

if(find_andoird() == true) { 

<p><strong>Android View</strong></p>
<p>
<img src=“accounting-android.png”  style="float:left;width:400px;height:600px;">
The android view is displayed here.  
</p>
}  

else { 

<p><strong>iPhone View</strong></p>
<p>
<img src=“accounting-iphone.png” style="float:left;width:400px;height:600px;">
The iphone view is displayed here.  
</p>
}

}

请帮我解决一下。感谢

2 个答案:

答案 0 :(得分:0)

此功能可能会有所帮助。

<?php

$user_agent     =   $_SERVER['HTTP_USER_AGENT'];

function getOS() { 

    global $user_agent;

    $os_platform    =   "Unknown OS Platform";

    $os_array       =   array(
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );

    foreach ($os_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }

    }   

    return $os_platform;

}

function getBrowser() {

    global $user_agent;

    $browser        =   "Unknown Browser";

    $browser_array  =   array(
                            '/msie/i'       =>  'Internet Explorer',
                            '/firefox/i'    =>  'Firefox',
                            '/safari/i'     =>  'Safari',
                            '/chrome/i'     =>  'Chrome',
                            '/opera/i'      =>  'Opera',
                            '/netscape/i'   =>  'Netscape',
                            '/maxthon/i'    =>  'Maxthon',
                            '/konqueror/i'  =>  'Konqueror',
                            '/mobile/i'     =>  'Handheld Browser'
                        );

    foreach ($browser_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $browser    =   $value;
        }

    }

    return $browser;

}


$user_os        =   getOS();
$user_browser   =   getBrowser();

$device_details =   "<strong>Browser: </strong>".$user_browser."<br /><strong>Operating System: </strong>".$user_os."";

print_r($device_details);

echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");

?>

答案 1 :(得分:0)

有一个名为detectizr(https://cdnjs.com/libraries/detectizr的modernizr扩展,你必须先将modernizr放在第一位)。它将为您添加标签操作系统,版本,浏览器,设备...然后您可以使用css显示例如:

sub("[^_]+_([A-Za-z]+)$", "_\\1", Multibyte)
#[1] "Sungpil_한성필__Han"