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

preg_replace轮换PHP代码

WBOY
Release: 2016-06-13 13:20:01
Original
1123 people have browsed it

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);

Copy after login


输出的结果总是不正确,总是把一些字符串当成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>
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