function getScreenWidth() {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/\b(?:iPhone|iPad|Android)\b/', $userAgent)) {
// Mobile devices
if (preg_match('/\bMobile\b/', $userAgent)) {
// For mobile devices, the screen width is not available directly, but we can use JavaScript to get it.
// You can include a JavaScript snippet in your HTML page and pass the value to PHP via AJAX.
return null;
} else {
// Tablets
if (preg_match('/\b(iPad)\b/', $userAgent)) {
return 768; // Assume iPad width
}
// Other tablets
return 1024; // Set a default tablet width
}
} else {
// Desktop devices
if (preg_match('/\bWindows\b/', $userAgent)) {
// Windows desktop
return 1920; // Set a default desktop width
} elseif (preg_match('/\bMacintosh\b/', $userAgent)) {
// Mac desktop
return 2560; // Set a default desktop width
} else {
// Other desktop devices
return 1366; // Set a default desktop width
}
}
}
// Usage
$screenWidth = getScreenWidth();
if ($screenWidth) {
echo "Screen width: " . $screenWidth . "px";
} else {
echo "Screen width is not available on mobile devices.";
}
No comments:
Post a Comment