Home  >  Article  >  Backend Development  >  preg_replace轮换PHP代码

preg_replace轮换PHP代码

WBOY
WBOYOriginal
2016-06-13 13:20:011101browse

preg_replace替换PHP代码
有一段PHP代码,我想让之间的字符全部变为大写

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
a = '<a><?php $str = \'0\';
if($str==\'0\'){
echo $str;
}
?></a>';

echo preg_replace("/<a>(.*?)/es",'"</a><a>".strtoupper("\\1")."</a>"',$a);



输出的结果总是不正确,总是把一些字符串当成PHP代码执行(例如输出的结果 $str 丢失),请问如何才能输出正确的结果?或者有没有其它的方法能实现?

谢谢了!

------解决方案--------------------
PHP code
$a = '<a><?php $str = \'0\';
if($str==\'0\'){
echo $str;
}
?></a>';
echo preg_replace_callback("/<a>(.*?)/s", 'foo', $a);
function foo($r) {
  return "</a><a>".strtoupper($r[1])."</a>";
} <div class="clear">
                 
              
              
        
            </div>
Statement:
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