wezom-agency/browserizr

Browserizr is tiny library, that detects your browser

3.0.2 2020-02-12 13:14 UTC

This package is not auto-updated.

Last update: 2024-04-18 10:18:14 UTC


README

license composer npm WezomAgency

Browserizr is tiny library, that detects your browser
with testing $_SERVER['HTTP_USER_AGENT'].

that is PHP port of the JavaScript library WezomAgency/Browserizr
with several limitations...

Install

composer require wezom-agency/browserizr

Prev versions

please read:

API

List of built-in tests

  1. Browserizr::detect()->isAndroid()
  2. Browserizr::detect()->isAndroid3()
  3. Browserizr::detect()->isAndroid4()
  4. Browserizr::detect()->isAndroid5()
  5. Browserizr::detect()->isAndroid6()
  6. Browserizr::detect()->isAndroid7()
  7. Browserizr::detect()->isAndroid8()
  8. Browserizr::detect()->isBlackberry()
  9. Browserizr::detect()->isBlackberry10()
  10. Browserizr::detect()->isEdge()
  11. Browserizr::detect()->isEdgeAndroid()
  12. Browserizr::detect()->isEdgeIOS()
  13. Browserizr::detect()->isIE()
  14. Browserizr::detect()->isIE8()
  15. Browserizr::detect()->isIE9()
  16. Browserizr::detect()->isIE10()
  17. Browserizr::detect()->isIE11()
  18. Browserizr::detect()->isIPad()
  19. Browserizr::detect()->isIPod()
  20. Browserizr::detect()->isIPhone()
  21. Browserizr::detect()->isWindowsPhone()
  22. Browserizr::detect()->isMoz()
  23. Browserizr::detect()->isOpera()
  24. Browserizr::detect()->isSafari()
  25. Browserizr::detect()->isChrome()
  26. Browserizr::detect()->isMobile()
  27. Browserizr::detect()->isDesktop()

Usage examples:

<?php

use WezomAgency\Browserizr;

?>
<?php if (Browserizr::detect()->isChrome()) { ?>
    <div class="alert">Chrome is here, baby!!!</div>
<?php } ?>

Generate css classes

Browserizr::detect()->cssClasses($tests, $cssPrefix = "", $toString = true): array|string

Create string with CSS classes

Parameters:

Name Data type Default value Description
$tests string[] array of wanted tests, each name - test name without is prefix
$cssPrefix string "" custom prefix for CSS class name
$toString bool true implode resulted array and return as string;

Usage examples:

<?php

use WezomAgency\Browserizr;

?>
<!DOCTYPE html>
<html class="<?= Browserizr::detect()->cssClasses(['Mobile', 'Desktop']); ?>">
    <head>...</head>
    <body>...</body>
</html>

<!-- render output -->
<html class="is-mobile is-not-desktop"><!-- if mobile device -->
<html class="is-not-mobile is-desktop"><!-- if desktop -->
<?php

use WezomAgency\Browserizr;

?>
<!DOCTYPE html>
<html class="<?= Browserizr::detect()->cssClasses(['Mobile', 'Desktop'], 'browserizr-'); ?>">
    <head>...</head>
    <body>...</body>
</html>

<!-- render output -->
<html class="browserizr-is-mobile browserizr-is-not-desktop"><!-- if mobile device -->
<html class="browserizr-is-not-mobile browserizr-is-desktop"><!-- if desktop -->
<?php

use WezomAgency\Browserizr;

$myCssClasses = Browserizr::detect()->cssClasses(['Mobile', 'Desktop'], '', false);
// ... work with array, e.g. array_push() or array_combine() or whatever you want

?>

Set custom UserAgent

by default Browserizr use $_SERVER['HTTP_USER_AGENT']
you can set own string for UserAgent.

Note
if you needed to change agent - you must do it before use the Browserizr tests

<?php

use WezomAgency\Browserizr;

Browserizr::detect()->setUserAgent('my own UserAgent string');

// then can test your browser correctly
var_export(Browserizr::detect()->isWindowsPhone());