有人可以告诉我如何让这段代码不显示下面未列出的设备上的应用下载按钮?
我知道可以做到,但我不太确定。
感谢。
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
switch(true) {
case stripos($ua,'android') :
$device = 'android'; break;
case stripos($ua,'ipad') :
$device = 'ipad'; break;
case stripos($ua,'iphone') :
$device = 'iphone'; break;
}
?>
<ul class="pageitem"><li class="button android"><input name="Submit" value="App Downloads" onclick="window.location='apps.php?id=<?php echo $device; ?>' " type="submit" /></li></ul>
答案 0 :(得分:2)
怎么样......
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
switch(true) {
case stripos($ua,'android') :
$device = 'android'; break;
case stripos($ua,'ipad') :
$device = 'ipad'; break;
case stripos($ua,'iphone') :
$device = 'iphone'; break;
default:
$device = 'unknown';
}
if($device != 'unknown')
{
?>
<ul class="pageitem"><li class="button android"><input name="Submit" value="App Downloads" onclick="window.location='apps.php?id=<?php echo $device; ?>' " type="submit" /></li></ul>
<?php } ?>
未经测试,我的PHP生锈了,所以不确定我是否正确使用了默认情况..
答案 1 :(得分:2)
试试这个,
<?php
$device='';
$ua=$_SERVER['HTTP_USER_AGENT'];
switch(true) {
case stripos($ua,'android') :
$device = 'android'; break;
case stripos($ua,'ipad') :
$device = 'ipad'; break;
case stripos($ua,'iphone') :
$device = 'iphone'; break;
}
if($device != '')
{
echo '<ul class="pageitem"><li class="button android">
<input name="Submit" value="App Downloads" onclick="window.location=\'apps.php?id='.$device.'\'" type="submit" />
</li></ul>';
}
?>
或者使用simple one
preg_match
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
if (preg_match('/android|ipad|iphone/i', $ua)) {
echo '<ul class="pageitem"><li class="button android">
<input name="Submit" value="App Downloads" onclick="window.location=\'apps.php?id='.$device.'\'" type="submit" />
</li></ul>';
}
?>
答案 2 :(得分:1)
尝试default
案例。如果不匹配,默认情况将起作用。
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
switch(true) {
case stripos($ua,'android') :
$device = 'android'; break;
case stripos($ua,'ipad') :
$device = 'ipad'; break;
case stripos($ua,'iphone') :
$device = 'iphone'; break;
default:
$device = false;
}
?>
<?php if($device): ?>
<ul class="pageitem"><li class="button android"><input name="Submit" value="App Downloads" onclick="window.location='apps.php?id=<?php echo $device; ?>' " type="submit" /></li></ul>
<?php endif; ?>