字符串中的 字符替换解决办法

WBOY
Release: 2016-06-13 13:40:47
Original
772 people have browsed it

字符串中的 字符替换
我现在有一些如这样的字符串

[3]/([2]*[2])

其中[3] [2] 分别对应我系统里的3号和2号变量 $e3 和$e2

希望将上面的式子通过程序转换成如下字符串

$e3/($e2*$e2)

这样就可以用eval计算了。

请问如何实现这个转换?有什么方法。



------解决方案--------------------
$str='[3]/([2]*[2])';
for($i=1;$i $str=str_replace('['.$i.']', '$e'.$i, $str);
}
echo $str;
?>
------解决方案--------------------

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;
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template