PHP Functions
PHP
<!-- Models/Front.php -->
<?php
namespace Models;
class Front {
public $db;
public function phonize($value){
return preg_replace('/[^0-9+]/', '', $value);
}
public function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
public function viberize($value){
if(self::isMobile()){
$viberPre = 'viber://add?number=';
$value = str_replace(array(' ','-','+','.','/'), '',$value);
}else{
$viberPre = 'viber://chat?number=';
$value = str_replace(array(' ','-','.','/'), '',$value);
}
return $viberPre.$value;
}
public function svg($svgName, $svgClassName = ''){
return '<svg class="icon '.$svgClassName.'">
<use xlink:href="/img/svgdefs.svg#'.$svgName.'" xmlns:xlink="http://www.w3.org/1999/xlink"></use>
</svg>';
}
public function ShowImage($thumbnail, $alt = SITE_NAME, $extra_class = ''){
$result = '';
$imgphp = '';
$altimg = !empty($alt) ? $alt : SITE_NAME;
$currentPath = $thumbnail;
if(substr_count($thumbnail, 'image=') > 0){
$currentPath = explode('image=', $thumbnail);
$imgphp = $currentPath[0].'image=';
$currentPath = count($currentPath) > 1 ? $currentPath[1] : '';
}
if(empty($currentPath) || $currentPath[0] !== '/') return $result;
$semideveloped_browsers = ['MSIE', 'Trident', 'Macintosh', 'iPad'];
$NO_WEBP = false;
$HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
foreach ($semideveloped_browsers as $item) {
if (substr_count($HTTP_USER_AGENT, $item) > 0) {
$NO_WEBP = true;
break;
}
}
$thumbnailARRAY = explode('/', $currentPath);
$pathOffset = count($thumbnailARRAY)-1;
$thumbnailWEBP = explode('.', end($thumbnailARRAY));
$fileOffset = count($thumbnailWEBP)-1;
if($pathOffset < 1) return $result;
if($fileOffset < 1) return $result;
$mime = strtolower($thumbnailWEBP[$fileOffset]);
$thumbnailWEBP[$fileOffset] = 'webp';
$thumbnailARRAY[$pathOffset] = implode('.', $thumbnailWEBP);
$newPath = implode('/', $thumbnailARRAY);
$tmp = $_SERVER['DOCUMENT_ROOT'].$newPath;
$webp = '';
if($NO_WEBP === false){
if (file_exists($tmp)) {
$webp = ' <source srcset=" '.$newPath.'" type="image/webp">';
}
}
$currentPath = $imgphp.$currentPath;
$result = '<picture class="'.$extra_class.'">
'.$webp.'
<source srcset="'.$currentPath.'" type="image/'.$mime.'">
<img src="'.$currentPath.'" alt="'.$altimg.'">
</picture>';
return $result;
}
}
PHP
<!-- init.php -->
<?php
include_once('Models/Front.php');
$fr = new \Models\Front();
?>
PHP
<a href="tel:<?= $fr->phonize($CPhone) ?>"><?= $CPhone ?></a>
<?= $fr->svg('icon_tel', 'scale') ?>
<?= $fr->ShowImage('/img/image.jpg', 'some image', 'some_class'); ?>