preg_filter用法

简介: preg_filter用法

(PHP 5 >= 5.3.0, PHP 7, PHP 8)


preg_filter — 执行一个正则表达式搜索和替换


preg_filter(

   mixed $pattern,

   mixed $replacement,

   mixed $subject,

   int $limit = -1,

   int &$count = ?

): mixed


preg_filter()等价于preg_replace()除了它仅仅返回(可能经过转化)与目标匹配的结果.


返回值


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


如果没有找到匹配或者发生了错误,当subject是数组 时返回一个空数组,其他情况返回null。


<?php
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4'); 
$pattern = array('/\d/', '/[a-z]/', '/[1a]/'); 
$replace = array('A:$0', 'B:$0', 'C:$0'); 
echo "preg_filter returns\n";
print_r(preg_filter($pattern, $replace, $subject)); 
echo "preg_replace returns\n";
print_r(preg_replace($pattern, $replace, $subject)); 
?>


效果:


a33dcbc428be46f7961ee81f4a4ecec9.png

相关文章
|
8月前
filter和find的区别
filter和find的区别
|
9月前
|
PHP
php正则表达式函数preg_replace替换span标签
php正则表达式函数preg_replace替换span标签
43 0
|
10月前
|
SQL
C++11之正则表达式(regex_match、regex_search、regex_replace)
C++11之正则表达式(regex_match、regex_search、regex_replace)
102 0
字符串匹配 正则表达式函数之match、test、exec、search、split、replace使用详解
字符串匹配 正则表达式函数之match、test、exec、search、split、replace使用详解
98 0
|
PHP
【PHP】preg_match函数
【PHP】preg_match函数
98 0
【PHP】preg_match函数
PHP 7.0.0中ereg_replace 函数使用preg_replace替换方法
PHP 7.0.0中ereg_replace 函数使用preg_replace替换方法
266 0
PHP 7.0.0中ereg_replace 函数使用preg_replace替换方法
|
PHP
preg_match_all用法
preg_match_all用法
142 0
preg_match_all用法
|
PHP
preg_match用法
preg_match用法
166 0
preg_match用法
|
PHP 索引
preg_grep用法
preg_grep用法
138 0
preg_grep用法