I want to manipulate a string like "... 4 + 3 (4-2) -...." to become "... 4 + 3 * (4-2) -....", but of course it should recognize any number , d, and then '(' and change it to 'd * ('. And I also want to change ') (' to ') * (' at the same time, if possible. It would be nice if you had the opportunity to add support for constants, such as pi or e.
At the moment, I'm just doing this stupidly:
private function make_implicit_multiplication_explicit($string)
{
$i=1;
if(strlen($string)>1)
{
while(($i=strpos($string,"(",$i))!==false)
{
if(strpos("0123456789",substr($string,$i-1,1)))
{
$string=substr_replace($string,"*(",$i,1);
$i++;
}
$i++;
}
$string=str_replace(")(",")*(",$string);
}
return $string;
}
But I believe that this can be done much better with preg_replace or some other regular expression function? But these guides are really cumbersome, I think.