Perl 相容语法函式库
preg_replace
字串比对剖析并取代。语法: 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");
?> 
 

解决方案 »

  1.   

    preg_replace : 
    函数格式:mixed preg_replace(mixed pattern, mixed replacement, mixed subject); 
        这个函数会将subject中符合表达式pattern的字符串全部替换为表达式replacement,返回结果字符串,subject内容不变。如果replacement中需要包含pattern中匹配得到的部分字符,则可以在pattern中使用小括号"()"把需要使用的部分括起来,在replacement中用"\\1"来读取。-------------------
    转自y10k的文章 并有部分修改.