PHP中正则表达式函数(Perl兼容)

时间:2022-12-03 19:16:29

PHP为使用Perl兼容的正则表达式搜索字符串提供了7个函数,分别是preg_grep()、preg_match()、preg_match_all()、preg_quote()、preg_replace()、preg_replace_callback()和preg_split()

1,  preg_grep()函数,将数组中符合要求的元素组成数组返回。函数样式如下,

array  preg_grep(string $pattern,array  $input),$pattern为查询条件,$input待查询数组。

如:

<?php
$foods = array("pasta","steak","fish","potatoes");
$food = preg_grep("/^p/",$foods);
var_dump($food);
?>

输出:

array(2) { [0]=> string(5) "pasta" [3]=> string(8) "potatoes" }

2,preg_split()函数,将字符串按照要求分割成数组返回

array preg_split(string $pattern,string $input,int $limit),$pattern分隔符,$input输入字符串,$limit为限定返回数组中元素个数。

如:

<?php
$delimitedText="Jason+++Gilmore++++++Columbus++++OH";
$fields = preg_split("/(\+)+/",$delimitedText);
var_dump($fields);
?>

输出:

array(4) { [0]=> string(5) "Jason" [1]=> string(7) "Gilmore" [2]=> string(8) "Columbus" [3]=> string(2) "OH" }

3,preg_match()函数,在字符串查找某个元素,存在返回true,否则返回false。

int preg_match(string $pattern,string $subject,$matches),$pattern查询的字符串,$subject待查询的字符串,$matches存放查询结果的数组,$matches[0]包含完整模式匹配到的文本,$matches[1]包含第一个捕获子组匹配到的文本,依次类推。

如:

$line = "Vim is the greatest word processor ever created! vim is greate";
if(preg_match("/\bvim\b/i",$line,$matches)){
    echo "字符串存在元素!";
}
var_dump($matches);

输出:

 字符串存在元素!array(1) { [0]=> string(3) "Vim" }

4,preg_match_all()函数,preg_match_all()与preg_match()功能类似,不过还是有差异。preg_match()结果匹配一次成功后停止匹配;preg_match_all()实现所有结果的匹配。

如:

$pattern = ‘/^(http:\/\/)?([^\/]+)/i‘;
$subject = ‘http://www.php.net/index.html‘;
preg_match_all($pattern,$subject,$match);
var_dump($match);

输出:

array (size=3)
  0 => 
    array (size=1)
      0 => string ‘http://www.php.net‘ (length=18)
  1 => 
    array (size=1)
      0 => string ‘http://‘ (length=7)
  2 => 
    array (size=1)
      0 => string ‘www.php.net‘ (length=11)

数组中第一个$matchs[0]就是整个$pattern匹配到的文本元素

$matchs[1] 匹配到的是子组 (http:\/\/) 匹配到的文本

$matchs[2] 匹配到的是子组(^\/+) 匹配到的文本

5,preg_replace()函数,在数组或字符串中查找并替换某部分。

mixed preg_replace(mixed $pattern,mixed $replacement,mixed $str,int $limit),$pattern替换值,$replacement被替换值,$str查找替换的字符串,$limit指定应当发生多少次匹配,不设置或设置为-1将替换所有出现的情况。$pattern和$replacement可以使数组,即$replacement[i]替换$pattern[i]。

如:

<?php
$text = "This is a link to http://www.wjgilmore.com/.";
echo preg_replace("/http:\/\/(.*)\//","<a href=\"\${0}\">\${0}</a>",$text);
?>

输出:

This is a link to http://www.wjgilmore.com/.

 如:

<?php
$draft = "In 2007 the company faced plummeting revenues and scandal";
$keywords = array("/faced/","/plummeting/","/scandal/");
$replacements = array("celebrated","skyrocketing","expansion");
echo preg_replace($keywords,$replacements,$draft);
?>

输出:

In 2007 the company celebrated skyrocketing revenues and expansion

6,preg_replace_callback()函数,此函数与preg_replace()函数类似,此处的被替换值$replacement可以用函数实现。

mixed preg_replace_callback(mixed $pattern,callback callback,mixed$str,int $limit)

如:

function acronym($matches){
    //通常: $matches[0]是完成的匹配
    //$matches[1]是第一个捕获子组的匹配
    //以此类推
    $acronyms = array(
        'WWW'=>'World Wide Web',
        'IRS'=>'Internal Revenue Service',
        'PDF'=>'Portable Document Format'
    );
    if(isset($acronyms[$matches[1]])){
        return $matches[1]."(".$acronyms[$matches[1]].")";
    }else{
        return $matches[1];
    }
}
$text = "The <acronym>IRS</acronym> offers tax forms in <acronym>PDF</acronym> format on the <acronym>WWW</acronym>";
$newtext = preg_replace_callback("/<acronym>(.*)<\/acronym>/U","acronym",$text);
print_r($newtext);
?>

输出:

The IRS(Internal Revenue Service) offers tax forms in PDF(Portable Document Format) format on the WWW(World Wide Web)

7,preg_quote()函数,在字符串中特殊字符前差一个反斜线。这些特殊字符包括$^*()+={}[]|\\:<>。其形式为:

string preg_quote(string $str,string $delimiter),$delimiter用于指定用于正则表达式的定界符,使用它也要用反斜线转义。

如:

<?php
$text = "Tickets for the bout are going for $500.";
echo preg_quote($text);
?>

输出:

Tickets for the bout are going for \$500\.