# PHP 函数:preg\_replace()

**URL:** <https://www.isharkfly.com/t/php-preg-replace/2800>\
**Category:** PHP\
**Created:** [October 27, 2012, 3:31am UTC](https://www.isharkfly.com/t/php-preg-replace/2800 "2012-10-27T03:31:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 27, 2012, 3:31am UTC](https://www.isharkfly.com/t/php-preg-replace/2800/1 "2012-10-27T03:31:43Z")

</div>

字符串比对解析并取代。

语法: mixed preg\_replace(mixed pattern, mixed replacement, mixed subject);

返回值: 混合类型资料

函数种类: 资料处理

内容说明  
本函数以 pattern 的规则来解析比对字符串 subject，欲取而代之的字符串为参数 replacement。返回值为混合类型资料，为取代后的字符串结果。

使用范例  
下例返回值为 $startDate = 6/19/1969

\<?php $patterns = array("/(19|20\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s\*{(\w+)}\s\*=/"); $replace = array("\\3/\\4/\\1", "$\\1 ="); print preg\_replace($patterns, $replace, "{startDate} = 1969-6-19"); ?\>
