您现在的位置是: 网站首页> PHP PHP

PHP常用正则表达式函数

Smile 2020-03-19 PHP 阅读:800

简介在PHP程序开发中,经常会有很多地方会用到正则表达式进行匹配和检索字符串,比如匹配电话号码,邮箱等,下面介绍PHP几个常用的正则表达式函数

  • preg_match:执行一个正则表达式匹配
  • preg_match_all:执行一个全局正则表达式匹配
  • preg_grep:返回匹配模式的数组条目
  • preg_replace:执行一个正则表达式的搜索和替换
  • preg_filter:执行一个正则表达式搜索和替换
  • preg_split:通过一个正则表达式分隔字符串

1、preg_match 查找匹配字符串 "hello"

语法:preg_match ( string $pattern , string $subject [array &$matches] )

$pattern:要搜索的模式,字符串类型,也就是正则表达式

$subject:输入字符串,被匹配的字符串

$matches:它将被填充为搜索结果。 $matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推

返回值:preg_match()返回 pattern 的匹配次数。 它的值将是0次(不匹配)或1次,因为preg_match()在第一次匹配后 将会停止搜索。preg_match_all()不同于此,它会一直搜索subject 直到到达结尾。 如果发生错误preg_match()返回 FALSE。

示例:

<?php 

$pattern = '/hello/';
$subject = 'hello world';

$result = preg_match($pattern,$subject,$arr);

if ($result) {
	echo "匹配到hello字符串<br/>";
}else{
	echo "没有匹配到hello字符串<br/>";
}

print_r($result);
print_r('<br>');
print_r($arr);

 ?>

输出:

匹配到hello字符串
1
Array ( [0] => hello )

2、preg_match_all  同样查找匹配字符串 "hello"

语法:preg_match_all ( string $pattern , string $subject [array &$matches] )

$pattern:要搜索的模式,字符串类型,也就是正则表达式

$subject:输入字符串,被匹配的字符串

$matches:多维数组,作为输出参数输出所有匹配结果

返回值:返回完整匹配次数(可能是0),或者如果发生错误返回FALSE。

示例:

<?php 

$pattern = '/hello/';
$subject = 'hello world hello php';

$result = preg_match_all($pattern,$subject,$arr);

if ($result) {
	echo "匹配到hello字符串<br/>";
}else{
	echo "没有匹配到hello字符串<br/>";
}

print_r($result);
print_r('<br>');
print_r($arr);

 ?>

输出:

匹配到hello字符串
2
Array ( [0] => Array ( [0] => hello [1] => hello ) )

3、preg_grep 查找匹配数字

语法:preg_grep ( string $pattern , array $input)

$pattern:要搜索的模式,字符串形,正则表达式

$input:输入数组

返回值:返回使用input中key做索引的数组

示例:

<?php 

$pattern = '/\d+/';
$input = [1,'php',3,6,'hello'];

$result = preg_grep($pattern,$input);
print_r($result);

 ?>

输出:

Array ( [0] => 1 [2] => 3 [3] => 6 )

4、preg_replace,preg_filter这两个函数比较相似, 搜索字符串"php"并替换

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

$pattern: 要搜索的模式,可以是字符串或一个字符串数组。

$replacement: 用于替换的字符串或字符串数组。

$subject: 要搜索替换的目标字符串或字符串数组。

$limit: 可选,对于每个模式用于每个 subject 字符串的最大可替换次数。 默认是-1(无限制)。

$count: 可选,为替换执行的次数。

返回值:

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

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

示例1:

<?php 

$pattern = '/\d/';
$subject='php12python345';
$replacement = '*';

$res1 = preg_replace($pattern,$replacement,$subject);
$res2 = preg_filter($pattern,$replacement,$subject);

print_r($res1);
print_r('<br>');
print_r($res2);

 ?>

输出:

php**python***
php**python***

这里preg_replace,preg_filter没有区别

示例2:

<?php 

$pattern = array('/[12]/','/[345]/');
$subject='php12python345';
$replacement = '@';

$res1 = preg_replace($pattern,$replacement,$subject);
$res2 = preg_filter($pattern,$replacement,$subject);

print_r($res1);
print_r('<br>');
print_r($res2);

 ?>

输出:

php@@python@@@
php@@python@@@

此时还是没有区别

示例3:

<?php 

$pattern = array('/[12]/','/[345]/');
$subject = array('a1','hjk','b2','abc','c345');
$replacement = array('@','#');

$res1 = preg_replace($pattern,$replacement,$subject);
$res2 = preg_filter($pattern,$replacement,$subject);

print_r($res1);
print_r('<br>');
print_r($res2);

 ?>

输出:

Array ( [0] => a@ [1] => hjk [2] => b@ [3] => abc [4] => c### )
Array ( [0] => a@ [2] => b@ [4] => c### )

此处就是两者的区别了

5、preg_split 分割字符串"hello,world,ha#php"

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

$pattern: 用于搜索的模式,字符串形式。

$subject: 输入字符串

$limit: 可选,如果指定,将限制分隔得到的子串最多只有limit个,返回的最后一个 子串将包含所有剩余部分。limit值为-1, 0或null时都代表"不限制", 作为php的标准,你可以使用null跳过对flags的设置。

flags:可以是任何下面标记的组合(以位或运算 | 组合):

PREG_SPLIT_NO_EMPTY

如果这个标记被设置, preg_split() 将仅返回分隔后的非空部分。

PREG_SPLIT_DELIM_CAPTURE

如果这个标记设置了,用于分隔的模式中的括号表达式将被捕获并返回。

PREG_SPLIT_OFFSET_CAPTURE

如果这个标记被设置, 对于每一个出现的匹配返回时将会附加字符串偏移量. 注意:这将会改变返回数组中的每一个元素, 使其每个元素成为一个由第0 个元素为分隔后的子串,第1个元素为该子串在subject 中的偏移量组成的数组。

示例:

<?php 

$pattern = '/[,#]/';
$subject='hello,world,ha#php';
$arr = preg_split($pattern,$subject);

print_r($arr);

 ?>

输出:

Array ( [0] => hello [1] => world [2] => ha [3] => php )

这个函数跟explode很相似,只不过preg_split 这个更强大

很赞哦! (0)

文章评论

站点信息