php正则替换

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:42:20934browse

$array = array('aaa','bbbbbb','cccccc','ddddddddddd');
$str = 'abcdej{$array}jdijij';

$str = preg_replace("/^\{\$(.*?)\}$/i",'print_r(\\1);',$str);
print_r($str);

如何替换成 {print_r($array)}    正则怎么写??
还有 for while foreach 等正则该怎么写???


回复讨论(解决方案)

$str = 'abcdej{$array}jdijij';$str = preg_replace('/\{(\$.+?)\}/', '<?php print_r(\\1);?>', $str);echo $str;
abcdejjdijij

规则串用双引号括起时要
$str = 'abcdej{$array}jdijij';$str = preg_replace("/\{(\\\$.+?)\}/", '<?php print_r(\\1);?>', $str);echo $str;

\\$ 和 \\\$  结果是一样的,我知道\ 是转义,但是两个就行了,但是很多地方都写得是三个,这是为什么?

这是测试得来的,比如我的环境就得写3个

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