Hello everyone,
First of all I just want to say that I never understood so well the regular expressions patterns, but I'm progressing 🙂
I want to create a function for replacing the "" char and left right matches with pow($1, $2). I manged to get to a point that is acceptable but the string that I receive from an API keeps getting bigger & bigger and now I'm stuck... it's like this:
$str = '(0+1*9^3+3)*(4+5)-(6/7)+(1+2)/(1+1)^((2/3)*3-1+(2/3))';
$str = preg_replace('/([0-9]+|\([0-9\+\-\*\/]+\)|[^\+\-\*\/]\([\S]+\))\^([0-9]+|\([0-9\+\-\*\/]+\)|[^\+\-\*\/]\([\S]+\))/', 'pow($1,$2)', $str);
echo $str;
This works ok to the given string but if i add 1 more "+(1+2)/(1+1)((2/3)*3-1+(2/3))'" to the end it doesn't work right.
Basically i want preg_replace to find the first "()" from the left & the first "()" from the right of "" char
Examples of how it should work (I'll only do for the left side of "" but it can applies as well on the right side)
3+23-2 => 3+pow(2, 3)-1
3+(1+1)32 => 3+pow((1+1), 3)1
3+(1+1+(1+2))3/2 => 3+pow((1+1+(1+2)), 3)/2
3+((3/3)+(2/2))2-1 => 3+pow(((3/3)+(2/2)), 2)-1
(3+1)3-1 => pow((3+1), 3)-1
etc...
Sort for all above is:
return as "$1" what is before "":
1. if the first thing before "" is int, return the number
2. if the first thing is ")" search for it's pair "(" and return everything inside them (something like '/(/([.*]))^/')
I'm sorry for my english, I hope you understood... and I hope that someone can help me on this problem 🙁
Thanks in advance!