常用的正则表达式处理函数

手册/FAQ (407) 2015-11-02 09:42:20

1,preg_match()函数。

函数preg_match()执行一个正则表达式匹配,其定义如下:

  1. int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) 

实际上就是搜索subject中匹配pattern的部分, 以保存在数组matches中.请看示例:

  1. <?php    
  2.  
  3.      $pattern = '/<b>.*?<\/b>/';    
  4.      $string = '<b>welcome</b> <b>to</b> <b>phpfuns</b>dsadsadas';    
  5.      if (preg_match($pattern, $string, $arr)) {    
  6.          echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>";    
  7.          print_r($arr);    
  8.      } else {    
  9.          echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>";    
  10.      }    
  11.  ?>   
  12.  

2,preg_match_all()函数。

函数preg_match_all()函数执行一个全局正则表达式匹配,其定义和preg_match()函数一致,只不过匹配了全部结果。请看示例:

  1. <?php    
  2.      $pattern = '/<b>.*?<\/b>/';    
  3.      $string = '<b>welcome</b> <b>to</b> <b>phpfuns</b>dsadsadas';    
  4.      if (preg_match_all($pattern, $string, $arr)) {    
  5.          echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>";    
  6.          print_r($arr);    
  7.      } else {    
  8.          echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>";    
  9.      }    
  10.  ?>   
  11.  

依旧是上面的示例(只换了正则处理函数为preg_match_all()),但是匹配的结果数组内容不一样了。

3, preg_replace()函数

函数preg_replace()执行一个正则表达式替换,其定义如下:

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

实际上就是搜索subject中匹配pattern的部分, 以replacement进行替换.其中limit指的是每个模式在每个subject上进行替换的最大次数. 默认是 -1(无限). 如果指定count,将会被填充为完成的替换次数.

注意:

A,如果subject是一个数组, preg_replace()返回一个数组, 其他情况下返回一个字符串.

B,如果匹配被查找到, 替换后的subject被返回, 其他情况下返回没有改变的subject. 如果发生错误, 返回NULL .

C,子模式可以应用到参数replacement中,使用方式为\n或者${n}。(在正则表达式的模式中我们只能使用\n的形式来获取已经匹配的子模式,切记!)

D,如果使用模式修正符e,则参数replacement中可以解析函数。(在其它的正则表达式处理函数中,模式修正符e均被忽略!)

请看下面的综合示例:

  1. <?php    
  2.     $pattern = '/(php)|(mysql)/e';    
  3.     $string = '这个字符串中的php和mysql被替换成大写的了!';    
  4.     $result = preg_replace($pattern, 'strtoupper("${1}\2")', $string, -1, $count);    
  5.     echo $result.'<br>';    
  6.     echo $count;    
  7. ?>   

      上例中,我们使用了模式修正符e,这样的话strtoupper()函数就可以当作字符串被解析,这就是模式修正符e的作用!而参数${1}和\2分别是子模式1和子模式2。上例的作用就是将字符串$string中匹配到的子模式php和mysql替换成大写字母!

4,preg_split()函数。

preg_split执行一个正则表达式分隔字符串。其定义如下:

array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

       实际上就是将subject按照pattern分割,返回分割后的数组。其中,limit将限制分隔得到的子串最多只有limit个, 返回的最后一个子串将包含所有剩余部分.limit值为-1, 0或null时都代表"不限制"。

我们来看一个示例:

  1. <?php    
  2.      $pattern = '/<p>(.*?)<\/p>/';    
  3.      $string = '这个字符串中的<p>php</p>和<p>mysql</p>被分割了!';    
  4.      $result = preg_split($pattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE);    
  5.      print_r($result);    
  6.  ?>   
  7.  

       上例中,我们使用了常量PREG_SPLIT_DELIM_CAPTURE设 置返回结果中包含子模式(如果设置为PREG_SPLIT_NO_EMPTY,preg_split()将进返回分隔后的非空部分。)我们如果把上例中正 则表达式的括号去掉,则结果中不再包含php和mysql这两个匹配成功的子模式。

THE END