Skip to content

Instantly share code, notes, and snippets.

@yetimdasturchi
Created August 11, 2022 15:45
Show Gist options
  • Save yetimdasturchi/62cdcfbc4f7b40f239f0c5ffc0769e24 to your computer and use it in GitHub Desktop.
Save yetimdasturchi/62cdcfbc4f7b40f239f0c5ffc0769e24 to your computer and use it in GitHub Desktop.
Markaziy bank kurs valyutalari uchun parser
<?php
function parse_cbu_exchange(){
$data = [];
$html = @file_get_contents('https://cbu.uz/oz/arkhiv-kursov-valyut/');
preg_match( "/<table class=\"table table-lined table_currency\">(.*?)<\/table>/isu" , $html , $matches);
if (!empty($matches[0])) {
preg_match_all("/<tr>(.*?)<\/tr>/isu", $matches[0], $tr);
if(!empty($tr[0])){
foreach ($tr[0] as $t) {
$c_name = "";
$c_icon = "";
$c_code = "";
$c_exchange = "";
$c_color = "";
$c_color_value = "";
$c_change_icon = "";
preg_match("/class=\"currency_name\">(.*?)<\/a>/isu", $t, $currency_name);
if (!empty($currency_name[1])) {
preg_match("/<span.*>(.*?)<\/span>/isu", $currency_name[1], $name);
if (!empty($name[1])) {
$c_name = trim($name[1]);
}
preg_match("/<svg.*>(.*?)<\/svg>/isu", $currency_name[1], $icon);
if (!empty($icon[0])) {
$c_icon = str_replace("/bitrix/templates/main/img/", "https://cbu.uz/bitrix/templates/main/img/", $icon[0]);
}
preg_match("/<td.*class=\"text-center\"\.*>([a-zA-Z]+)<\/td>/isu", $t, $code);
if (!empty($code[1])) {
$c_code = trim($code[1]);
}
preg_match("/<span.*class=\"currency_exchange\"\.*>(.*)<\/span>/isu", $t, $exchange);
if (!empty($exchange[1])) {
$c_exchange = trim($exchange[1]);
}
preg_match("/<div.*class=\"currency_change\"\.*>(.*)<\/div>/isu", $t, $currency_change);
if (!empty($currency_change[0])) {
preg_match("/<span.*>(.*?)<\/span>/isu", $currency_change[0], $color_value);
if (!empty($color_value[1])) {
$c_color_value = trim($color_value[1]);
}
preg_match("/<span.*class=\"color_(.*?)\"\.*>.*?<\/span>/isu", $currency_change[0], $color);
if (!empty($color[0])) {
$c_color = trim($color[1]);
}
preg_match("/<svg.*>(.*?)<\/svg>/isu", $currency_change[0], $change_icon);
if (!empty($change_icon[0])) {
$c_change_icon = str_replace("/bitrix/templates/main/img/", "https://cbu.uz/bitrix/templates/main/img/", $change_icon[0]);
}
}
}
if (
!empty( $c_name ) &&
!empty( $c_icon ) &&
!empty( $c_code ) &&
!empty( $c_exchange ) &&
!empty( $c_color ) &&
!empty( $c_color_value ) &&
!empty( $c_change_icon )
) {
if ($c_color == 'green') {
$c_color = 'up';
}else if ($c_color == 'red'){
$c_color = 'down';
}else{
$c_color = 'nothing';
}
$data[] = [
'name' => $c_name,
'icon' => $c_icon,
'code' => $c_code,
'exchange' => $c_exchange,
'status' => [$c_color, $c_color_value, $c_change_icon],
];
}
}
}
}
return !empty($data) ? $data : FALSE;
}
print_r( parse_cbu_exchange() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment