Replace the characters in a string. I now have some strings like this [3]/([2] * [2]). [3] [2] corresponds to variables 3 and 2 in our system. $ e3 and $ e2 want to convert the above formula into the following string through the program $ e3/($ e2 * $ e2) in this way, you can use eval for calculation. How can this conversion be implemented? What are the methods ., & Lt ;? Php $ str = '[3]/([2] character replacement in string
I now have some strings like this
[3]/([2] * [2])
[3] [2] correspond to the variables $ e3 and $ e2 in our system respectively.
You want to convert the above formula into the following string through the program
$ E3/($ e2 * $ e2)
In this way, you can use eval for calculation.
How can this conversion be implemented? What are the methods.
------ Solution --------------------
$ Str = '[3]/([2] * [2])';
For ($ I = 1; $ I <= 100; $ I ++ ){
$ Str = str_replace ('['. $ I. ']', '$ e'. $ I, $ str );
}
Echo $ str;
?>
------ Solution --------------------
C/C ++ code
$ Str = '[3]/([2] * [2])'; echo procnum ($ str); function procnum ($ str) {// $ I = 0; while (preg_match ('/(\ [\ d + \])/', $ str) {$ str = preg_replace ('/\ [(\ d +) \]/', "\ $ e \ $1", $ str, 1); // if ($ I ++> 100) break;} return $ str ;}