denissimon / formula-parser

Parsing and evaluating mathematical formulas given as strings.
MIT License
76 stars 24 forks source link

strstr(): Empty needle #5

Closed docrud closed 5 years ago

docrud commented 5 years ago

Hi there,

I got a error like:

[2]strstr(): Empty needle[.../vendor/denissimon/formula-parser/FormulaParser.php:301

And I found this:

private function combine($array) {
        $new_array = [];
        $i = $j = $key = 0;

        foreach ($array as $item) {

            $array_ip1 = (isset($array[$i+1])) ? $array[$i+1] : null;

            end($new_array);
            $cur = current($new_array);

            if ((@strstr('+-', (string) $item)) && (@strstr('+-', (string) $cur)) 
            && (@strstr('+-', (string) $array_ip1))) {
                if ($item === '-')
                    $new_array[key($new_array)] = ($cur == '+') ? '-' : '+';
            } elseif ((@strstr('+-', (string) $item)) && (@strstr('*/^', (string) $cur)) 
            && (@strstr('+-', (string) $array_ip1))) {
                $new_array[] = $item;
            } else {
                if ((@strstr('+-', (string) $item)) && ($key+1 != $i) 
                && (@strstr('*/^', (string) $array[$key]))
                && (!is_numeric($array[$key+1])) && (isset($array[$key+1]))) {
                    if ($item === '-')
                        $new_array[key($new_array)] = ($cur == '+') ? '-' : '+';
                } else {
                    $new_array[] = $item;
                    $key = $i;
                }
            }
            $i++;
        }
// ...
}

when foreach run, $new_array is empty, and (string) $cur got a empty string.

so strstr() give me a error.

denissimon commented 5 years ago

Hi! Please try this code (instead of 301-306 lines in the combine() method) and let me know if it works without any warnings:

if ((@strstr('+-', (string) $item)) && (!empty($cur) && @strstr('+-', (string) $cur)) 
&& (!empty($array_ip1) && @strstr('+-', (string) $array_ip1))) {
    if ($item === '-')
        $new_array[key($new_array)] = ($cur == '+') ? '-' : '+';
} elseif ((@strstr('+-', (string) $item)) && (!empty($cur) && @strstr('*/^', (string) $cur)) 
&& (!empty($array_ip1) && @strstr('+-', (string) $array_ip1))) {
docrud commented 5 years ago

Hi!

It works without any warnings.

Thank your for your quickly reply.

denissimon commented 5 years ago

Great, thanks!