Home > Backend Development > PHP Tutorial > php正则替换

php正则替换

WBOY
Release: 2016-06-23 13:42:20
Original
931 people have browsed it

$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;
Copy after login
abcdejjdijij

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

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

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

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