(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)
preg_replace_callback — 执行一个正则表达式搜索并且使用一个回调进行替换
$pattern,$callback,$subject,$limit = -1,&$count = null ,$flags = 0
这个函数的行为除了可以指定一个
callback 替代
replacement
进行替换字符串的计算,其他方面等同于
preg_replace() 。
pattern要搜索的模式,可以是字符串或一个字符串数组。
callback
一个回调函数,在每次需要替换时调用,调用时函数得到的参数是从 subject
中匹配到的结果。回调函数返回真正参与替换的字符串。这是该回调函数的签名:
经常会需要 callback 函数而仅用于
preg_replace_callback() 一个地方的调用。在这种情况下,你可以使用
匿名函数 来定义一个匿名函数作为
preg_replace_callback()
调用时的回调。 这样做你可以保留所有调用信息在同一个位置并且不会因为一个不在任何其他地方使用的回调函数名称而污染函数名称空间。
示例 #1 preg_replace_callback() 和 匿名函数
<?php
/* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写。 */
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!feof($fp)) {
$line = fgets($fp);
$line = preg_replace_callback(
'|<p>\s*\w|',
function ($matches) {
return strtolower($matches[0]);
},
$line
);
echo $line;
}
fclose($fp);
?>subject要搜索替换的目标字符串或字符串数组。
limit
对于每个模式用于每个 subject 字符串的最大可替换次数。
默认是 -1(无限制)。
count如果指定,这个变量将被填充为替换执行的次数。
flags
flags 可以是 PREG_OFFSET_CAPTURE
和 PREG_UNMATCHED_AS_NULL 标志的组合, 这会影响匹配到的结果的格式。
相关详情请参阅 preg_match() 中的描述。
如果 subject 是一个数组,
preg_replace_callback() 返回一个数组,其他情况返回字符串。错误发生时返回 null 。
如果查找到了匹配,返回替换后的目标字符串(或字符串数组),其他情况
subject 将会无变化返回。
如果传递的正则表达式无法正常解析,会发出 E_WARNING 。
| 版本 | 说明 |
|---|---|
| 7.4.0 |
新增 flags 参数。
|
示例 #2 preg_replace_callback()示例
<?php
// 将文本中的年份增加一年.
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 回调函数
function next_year($matches)
{
// 通常: $matches[0]是完成的匹配
// $matches[1]是第一个捕获子组的匹配
// 以此类推
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"|(\d{2}/\d{2}/)(\d{4})|",
"next_year",
$text);
?>以上示例会输出:
April fools day is 04/01/2003 Last christmas was 12/24/2002
示例 #3 preg_replace_callback() 使用递归构造处理 BB 码的封装
<?php
$input = "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain";
function parseTagsRecursive($input)
{
/* 译注: 对此正则表达式分段分析
* 首尾两个#是正则分隔符
* \[indent] 匹配一个原文的[indent]
* ((?:[^[]|\[(?!/?indent])|(?R))+)分析:
* (?:[^[]|\[(?!/?indent])分析:
* 首先它是一个非捕获子组
* 两个可选路径, 一个是非[字符, 另一个是[字符但后面紧跟着不是/indent或indent.
* (?R) 正则表达式递归
* \[/indent] 匹配结束的[/indent]
* /
$regex = '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#';
if (is_array($input)) {
$input = '<div style="margin-left: 10px">'.$input[1].'</div>';
}
return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}
$output = parseTagsRecursive($input);
echo $output;
?>
The easiest way to pass more than one parameters to the callback function is with the 'use' keyword.
[This is better than using global, because it works even when we are already inside a function.]
In this example, the callback function is an anonymous function, which takes one argument, $match, supplied by preg_replace_callback(). The extra
"use ($ten)" puts the $ten variable into scope for the function.
<?php
$string = "Some numbers: one: 1; two: 2; three: 3 end";
$ten = 10;
$newstring = preg_replace_callback(
'/(\\d+)/',
function($match) use ($ten) { return (($match[0] + $ten)); },
$string
);
echo $newstring;
#prints "Some numbers: one: 11; two: 12; three: 13 end";
?>preg_replace_callback returns NULL when pcre.backtrack_limit is reached; this sometimes occurs faster then you might expect. No error is raised either; so don't forget to check for NULL yourselfIf you want to call non-static function inside your class, you can do something like this.
For PHP 5.2 use second argument like array($this, 'replace'):
<?php
class test_preg_callback{
private function process($text){
$reg = "/\{([0-9a-zA-Z\- ]+)\:([0-9a-zA-Z\- ]+):?\}/";
return preg_replace_callback($reg, array($this, 'replace'), $text);
}
private function replace($matches){
if (method_exists($this, $matches[1])){
return @$this->$matches[1]($matches[2]);
}
}
}
?>
For PHP 5.3 use second argument like "self::replace":
<?php
class test_preg_callback{
private function process($text){
$reg = "/\{([0-9a-zA-Z\- ]+)\:([0-9a-zA-Z\- ]+):?\}/";
return preg_replace_callback($reg, "self::replace", $text);
}
private function replace($matches){
if (method_exists($this, $matches[1])){
return @$this->$matches[1]($matches[2]);
}
}
}
?>A simple function to replace a list of complete words or terms in a string (for PHP 5.3 or above because of the closure):
<?php
function replace_words($list, $line, $callback) {
return preg_replace_callback(
'/(^|[^\\w\\-])(' . implode('|', array_map('preg_quote', $list)) . ')($|[^\\w\\-])/mi',
function($v) use ($callback) { return $v[1] . $callback($v[2]) . $v[3]; },
$line
);
}
?>
Example of usage:
<?php
$list = array('php', 'apache web server');
$str = "php and the apache web server work fine together. php-gtk, for example, won't match. apache web servers shouldn't too.";
echo replace_words($list, $str, function($v) {
return "<strong>{$v}</strong>";
});
?>it is much better on preformance and better practice to use the preg_replace_callback function instead of preg_replace with the e modifier.
function a($text){return($text);}
// 2.76 seconds to run 50000 times
preg_replace("/\{(.*?)\}/e","a('\1円','\2円','\3円',\$b)",$a);
// 0.97 seconds to run 50000 times
preg_replace_callback("/\{(.*?)\}/s","a",$a);<?php
// Nice little function that convert a string to uppercase by keeping the HTMLentities intact.
public static function strtoupper_entities($str) {
$patternMajEntities = '/(\&([A-Z])(ACUTE|CEDIL|CARON|CIRC|GRAVE|ORN|RING|SLASH|TH|TILDE|UML)\;)+/';
$str = preg_replace_callback ($patternMajEntities,
function ($matches) {
return "&" . $matches[2] . strtolower($matches[3]) . ";";
}, strtoupper($str));
return $str;
}The good version of the class PhpHex2Str
<?php
class PhpHex2Str
{
private $strings;
private static function x_hex2str($hex) {
$hex = substr($hex[0], 1);
$str = '';
for($i=0;$i < strlen($hex);$i+=2) {
$str.=chr(hexdec(substr($hex,$i,2)));
}
return $str;
}
public function decode($strings = null) {
$this->strings = (string) $strings;
return preg_replace_callback('#\%[a-zA-Z0-9]{2}#', 'PhpHex2Str::x_hex2str', $this->strings);
}
}
// Exemple
$obj = new PhpHex2Str;
$strings = $obj->decode($strings);
var_dump($strings);
?>Text lines numeration:
<?PHP
// Multieline text:
$Text = "
Some
Multieline
text
for
numeration";
// For count:
$GLOBALS["LineNUMBER"] = 1;
// Replace linestart on number:
PRINT preg_replace_callback("/^/m",function ()
{
return $GLOBALS["LineNUMBER"]++." ";
},
$Text);
?>
1
2 Some
3 Multieline
4 text
5 for
6 numerationFrom PHP 5.3 you can use an anonymous function to pass local variables into the callback.
<?php
public function replace_variables( $subject, $otherVars ) {
$linkPatterns = array(
'/(<a .*)href=(")([^"]*)"([^>]*)>/U',
"/(<a .*)href=(')([^']*)'([^>]*)>/U"
);
$callback = function( $matches ) use ( $otherVars ) {
$this->replace_callback($matches, $otherVars);
};
return preg_replace_callback($this->patterns, $callback, $subject);
}
public function replace_callback($matches, $otherVars) {
return $matches[1] . $otherVars['myVar'];
}
?>The pcre.backtrack_limit option (added in PHP 5.2) can trigger a NULL return, with no errors. The default pcre.backtrack_limit value is 100000. If you have a match that exceeds about half this limit it triggers a NULL response.
e.g. My limit was at 100000 but 500500 triggered a NULL response. I'm not running unicode but I *guess* PCRE runs in utf-16.Please note! if you have defined namespace,
the usage format must me changed:
echo preg_replace_callback(
"|(\d{2}/\d{2}/)(\d{4})|",
__NAMESPACE__ . '\\next_year',
$text);<?php
$format = <<<SQL
CREATE DATABASE IF NOT EXISTS :database;
GRANT ALL PRIVILEGES ON :database_name.* TO ':user'@':host';
SET PASSWORD = PASSWORD(':pass');
SQL;
$args = ["database"=>"people", "user"=>"staff", "pass"=>"pass123", "host"=>"localhost"];
preg_replace_callback("/:(\w+)/", function ($matches) use ($args) {
return @$args[$matches[1]] ?: $matches[0];
}, $format);
/*
Result:
CREATE DATABASE IF NOT EXISTS people;
GRANT ALL PRIVILEGES ON :database_name.* TO 'staff'@'localhost';
SET PASSWORD = PASSWORD('pass123');
The `:database_name` placeholder doesn't exist as a matching key in `$args` so it's returned as is.
This way you know you need to correct the array by adding the "database_name" item.
*/