我有一个包含数学表达式的字符串,如(21)*(4+2)
。出于计算的目的,我需要“简化”它,使它在表达式之间不包含任何数字(即(21)*(4+2) => 21*(4+2)
)。我不知道该怎么做(我想到了正则表达式取代的东西,但我不太擅长处理它。)
答案 0 :(得分:0)
你可以做一个像这样的算法:
$str = "(21)*(4+2)";
//split above to array of characters
$arr = str_split($str);
foreach($arr as $i => $char) {
if character is opening parenthesis {
get all characters in a string until closing parnethesis is found
endif }
if the string you received from above contains only digits
(means it has no expression i.e. +,-,/,%,*) then remove the first and last
characters of the above string which are the parenthesis and append the
string to the final string.
}
答案 1 :(得分:0)