top of page
  • Фото автораJavid Karimov

PHP-də köməyinizə çata biləcək funksiyalar


Bu blogda bir neçə funksiya yazacağıq ki, onlar kod yazan zaman sizin köməyinizə çata bilər.


Highlight funksiyası

Hərdən saytda nəyisə axtarış verərkən onun arxa fonun rəngləməyə ehtiyac duyulur. Məhz bu zaman bu highlight funksiyası kömək ola bilər. Bu sadə funksiya olub aşağıdakı kimi yazılır:

function highlight($text_highlight, $text_search, $bg_color = '#FFFF66', $text_color = '#FF0000')
{
    $str = preg_replace('#'. preg_quote($text_highlight) .'#i', '.$bg_color.'; color:'.$text_color.';">\\0', $text_search);
    return $str;
}

Konfiqurasiyanı oxuya bilmək.

Burada "dot notation" prinsipi tətbiq olunur. Beləki siz bu halda multimassivli konfiqurasiyanı . (nöqtə) ilə oxuya bilərsiniz. Funksiya aşağıdakı kimidir:


if(!function_exists('config')) {
    function config($key, $file = 'site')
    {
        $array = include 'konfiqurasiya faylina geden tam yol/' . $file . '.php';
        $path = explode('.', $key);
        $temp = &$array;
        foreach ($path as $keyx) {
            $temp =& $temp[$keyx];
        }
        return (($temp) == NULL || is_array($temp)) ? $file . '.error.' . $key : $temp;
    }
}

Nümunə:

// site.php fayli:
return [
'one' => '1',
'two' => [
    'iki' => 2
]
];

Bu funksiya ilə konfiqurasiyanı oxumaq qaydası:

config('one'); // netice: 1
config('two.iki'); // netice 2

Valyuta konvertasiya funksiyası:

if(!function_exists('currencyConverter')) {
    function currencyConverter($amount,$from,$to,$number_format = 0) {
        $converted = ($amount*$from/$to);
        if($number_format > 0) $converted = number_format($converted,$number_format);
        return $converted;
    }
}

Burada:

$amount - məbləğ
$from - mövcud valyutanın dəyəri
$to - çevriləcək valyutanın dəyəri
$number_format - number_format() funksiyasında tam ədəddən sonra gələcək kəsr sayıdır.

20 просмотров0 комментариев

Недавние посты

Смотреть все
bottom of page