PHP Classes

File: helper/bc.php

Recommend this page to a friend!
  Classes of Alexey Starikov   PHP Helper Class   helper/bc.php   Download  
File: helper/bc.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PHP Helper Class
Call helper functions that are loaded dynamically
Author: By
Last change:
Date: 5 years ago
Size: 2,468 bytes
 

Contents

Class file image Download
<?php
# php >5.5 preg_replace ??????????? /e ??????????
# ?????? ?????? ??? ?????? ?????? ? php 7 ??? ?? ????????
# H::bc('(1+2)-1'); H::bc('($1+$2)-$3','1','2','1') ?????? ???????????, ?????? ???????? ? ??????? ????? ???????.
# ????? ?????? ? ??????? ? ?????? ?? ??? ??? ???? ??? ?? ?????????
return function () {
   
$argv = func_get_args();
   
$string = str_replace(' ', '', "({$argv[0]})");
   
$operations = array();
    if (
strpos($string, '^') !== false) $operations[] = '\^';
    if (
strpbrk($string, '*/%') !== false) $operations[] = '[\*\/\%]';
    if (
strpbrk($string, '+-') !== false) $operations[] = '[\+\-]';
    if (
strpbrk($string, '<>!=') !== false) $operations[] = '<|>|=|<=|==|>=|!=|<>';
   
//$string = preg_replace('/\$([0-9\.]+)/e', '$argv[$1]', $string);
   
$string = preg_replace_callback('/\$([0-9\.]+)/', function($m) use($argv){
                return
$argv[$m[1]];
            },
$string);
    while (
preg_match('/\(([^\)\(]*)\)/', $string, $match)) {
      
// print_r($match);
       
foreach ($operations as $operation) {
            if (
preg_match("/([+-]{0,1}[0-9\.]+)($operation)([+-]{0,1}[0-9\.]+)/", $match[1], $m)) {
                switch(
$m[2]) {
                    case
'+': $result = bcadd($m[1], $m[3]); break;
                    case
'-': $result = bcsub($m[1], $m[3]); break;
                    case
'*': $result = bcmul($m[1], $m[3]); break;
                    case
'/': $result = bcdiv($m[1], $m[3]); break;
                    case
'%': $result = bcmod($m[1], $m[3]); break;
                    case
'^': $result = bcpow($m[1], $m[3]); break;
                    case
'==':
                    case
'=': $result = bccomp($m[1], $m[3]) == 0; break;
                    case
'>': $result = bccomp($m[1], $m[3]) == 1; break;
                    case
'<': $result = bccomp($m[1], $m[3]) ==-1; break;
                    case
'>=': $result = bccomp($m[1], $m[3]) >= 0; break;
                    case
'<=': $result = bccomp($m[1], $m[3]) <= 0; break;
                    case
'<>':
                    case
'!=': $result = bccomp($m[1], $m[3]) != 0; break;
                }
               
$match[1] = str_replace($m[0], $result, $match[1]);
            }
        }
       
$string = str_replace($match[0], $match[1], $string);
    }
    return
$string;
};
?>