PHP 8.4.2 Released!

preg_replace_callback

(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)

preg_replace_callback正規表現検索を行い、コールバック関数を使用して置換を行う

説明

preg_replace_callback(
    string|array $pattern,
    callable $callback,
    string|array $subject,
    int $limit = -1,
    int &$count = null,
    int $flags = 0
): string|array|null

この関数の動作は、ほぼ preg_replace() と同じですが、 replacement の代わりに callback を指定するところが異なります。

パラメータ

pattern

検索するパターン。 文字列あるいは文字列の配列のいずれかとなります。

callback

このコールバック関数は、検索対象文字列でマッチした要素の配列が指定されて コールされます。このコールバック関数は、置換後の文字列を返す必要があります。 コールバックのシグネチャは、次のとおりです。

handler(array $matches): string

preg_replace_callback() 用の callback 関数が、 ひとつの場所だけで必要となることがあります。 そんな場合は、 無名関数 を使って宣言した無名関数を preg_replace_callback() のコール時に使用します。 このようにすることにより、コールに関するすべての情報を 1 ヶ所に集め、 他の場所で使用されないコールバック関数名で関数の名前空間を 汚染しないようにすることができます。

例1 preg_replace_callback() と無名関数

<?php
/* Unix 方式のコマンドラインフィルタです。
* 段落の冒頭の大文字を、小文字に変換します。*/
$fp = fopen("php://stdin", "r") or die("標準入力から読み込めません");
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_CAPTUREPREG_UNMATCHED_AS_NULL の組み合わせが指定できます。 これは matches 配列のフォーマットに影響します。 詳細は preg_match() 関数の説明を参照ください。

戻り値

preg_replace_callback() は、 subject が配列の場合には配列を、 それ以外の場合は文字列を返します。 エラー時の戻り値は null となります。

マッチするものが見つかった場合は新しい subject を返し、それ以外の場合はもとの subject をそのまま返します。

エラー / 例外

渡された正規表現のパターンがコンパイルできない場合、E_WARNING が発生します。

変更履歴

バージョン 説明
7.4.0 パラメータ flags が追加されました。

例2 preg_replace_callback() の例

<?php
// このテキストは 2002 に使われていたものなのですが、
// これを 2003 年対応の日付に変更したいのです
$text = "エイプリルフールの日付は 04/01/2002 です\n";
$text.= "この前のクリスマスの日付は 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);

?>

上の例の出力は以下となります。

エイプリルフールの日付は 04/01/2003 です
この前のクリスマスの日付は 12/24/2002 でした

例3 カプセル化された BB code を処理するための、 preg_replace_callback() での再帰構造の使用

<?php
$input
= "通常の位置 [indent] 字下げ [indent] もっと字下げ [/indent] 字下げ [/indent] 通常の位置";

function
parseTagsRecursive($input)
{

$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;
?>

参考

add a note

User Contributed Notes 22 notes

up
104
Richard
12 years ago
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";
?>
up
7
Sjon at hortensius dot net
17 years ago
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 yourself
up
24
Yuri
12 years ago
If 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]);
}
}
}
?>
up
3
carlos dot ballesteros at softonic dot com
15 years ago
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>";
});
?>
up
2
Drake
14 years ago
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);
?>
up
2
matt at mattsoft dot net
18 years ago
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);
up
5
Fredow
9 years ago
<?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;
}
up
2
T-Soloveychik at ya dot ru
11 years ago
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 numeration
up
4
development at HashNotAdam dot com
12 years ago
From 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'];
}
?>
up
1
kkatpki
11 years ago
Note that, as of PHP 5.3, it seems that named subpatterns are now included in the matches array by their named key as well as their numerical key.

To build off of Chris' previous example, as of PHP 5.3, you *can* do

<?php

preg_replace_callback
('/(?<char>[a-z])/', 'callback', 'word');

function
callback($matches) {
var_dump($matches);
}

?>

and expect to get $matches['char'] in your function. * BUT ONLY AS OF PHP 5.3 *

Please be mindful of this if you intend to support PHP 5.2.
up
1
Florian Arndt
12 years ago
This small class allows PHP users to read JSON files with include statements in them. For instance the include {{{ "relative/to/including.json" }}} is replaced by the content of the json file located at "relative/to/including.json".

<?php
/**
* Handles JSON files with includes
* Purpose: handle bigger JSON files by featuring "includes"
*
* @author Florian Arndt
*/
class JWI {
/**
* Parses a JSON file and returns its contents
* @param String $filename
*/
static function read($filename) {
if(!
file_exists($filename))
throw new
Exception('<b>JWI Error: JSON file <tt>'.$filename.'</tt> not found!</b>');
$content = join('', file($filename));
$dir = dirname($filename);
/**
* replace
* include statements
* with
* content of the file to include
* recursively
*/
$content = preg_replace_callback(
'/{{{\s*"\s*(.+)\s*"\s*}}}/', // >include file< - pattern
create_function(
'$matches', // callback parameter
sprintf(
'$fn = "%s/".$matches[1];'.
'return JWI::read($fn);',
realpath(dirname($filename))
)
),
$content
);
return
$content;
}
}
up
1
chris at ocproducts dot com
14 years ago
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.
up
1
Anonymous
14 years ago
Created this to fetch the link and name of an anchor tag. I use this when cleaning an HTML email to text. Using regex for HTML is not recommended but for this purpose I see no issue with it. This is not designed to work for nested anchors.

A note to keep in mind:
I was primarily concerned with valid HTML so if attributes do no use ' or " to contain the values then this will need to be tweaked.
If you can edit this to work better, please let me know.
<?php
/**
* Replaces anchor tags with text
* - Will search string and replace all anchor tags with text (case insensitive)
*
* How it works:
* - Searches string for an anchor tag, checks to make sure it matches the criteria
* Anchor search criteria:
* - 1 - <a (must have the start of the anchor tag )
* - 2 - Can have any number of spaces or other attributes before and after the href attribute
* - 3 - Must close the anchor tag
*
* - Once the check has passed it will then replace the anchor tag with the string replacement
* - The string replacement can be customized
*
* Know issue:
* - This will not work for anchors that do not use a ' or " to contain the attributes.
* (i.e.- <a href=http: //php.net>PHP.net</a> will not be replaced)
*/
function replaceAnchorsWithText($data) {
/**
* Had to modify $regex so it could post to the site... so I broke it into 6 parts.
*/
$regex = '/(<a\s*'; // Start of anchor tag
$regex .= '(.*?)\s*'; // Any attributes or spaces that may or may not exist
$regex .= 'href=[\'"]+?\s*(?P<link>\S+)\s*[\'"]+?'; // Grab the link
$regex .= '\s*(.*?)\s*>\s*'; // Any attributes or spaces that may or may not exist before closing tag
$regex .= '(?P<name>\S+)'; // Grab the name
$regex .= '\s*<\/a>)/i'; // Any number of spaces between the closing anchor tag (case insensitive)

if (is_array($data)) {
// This is what will replace the link (modify to you liking)
$data = "{$data['name']}({$data['link']})";
}
return
preg_replace_callback($regex, 'replaceAnchorsWithText', $data);
}

$input = 'Test 1: <a href="http: //php.net1">PHP.NET1</a>.<br />';
$input .= 'Test 2: <A name="test" HREF=\'HTTP: //PHP.NET2\' target="_blank">PHP.NET2</A>.<BR />';
$input .= 'Test 3: <a hRef=http: //php.net3>php.net3</a><br />';
$input .= 'This last line had nothing to do with any of this';

echo
replaceAnchorsWithText($input).'<hr/>';
?>
Will output:
Test 1: PHP.NET1(http: //php.net1).
Test 2: PHP.NET2(HTTP: //PHP.NET2).
Test 3: php.net3 (is still an anchor)
This last line had nothing to do with any of this
up
1
Evgeny
1 year ago
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);
up
0
steven at nevvix dot com
6 years ago
<?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.
*/
up
-2
2962051004 at qq dot com
6 years ago
<?php

/**
* 将中文转为Html实体
* Turning Chinese into Html entity
* Author QiangGe
* Mail 2962051004@qq.com
*
*/

$str = <<<EOT
你好 world
EOT;

function
ChineseToEntity($str) {
return
preg_replace_callback(
'/[\x{4e00}-\x{9fa5}]/u', // utf-8
// '/[\x7f-\xff]+/', // if gb2312
function ($matches) {
$json = json_encode(array($matches[0]));
preg_match('/\[\"(.*)\"\]/', $json, $arr);
/*
* 通过json_encode函数将中文转为unicode
* 然后用正则取出unicode
* Turn the Chinese into Unicode through the json_encode function, then extract Unicode from regular.
* I think this idea is seamless.
*/
return '&#x'. str_replace('\\u', '', $arr[1]). ';';
},
$str
);
}

echo
ChineseToEntity($str);
// &#x4f60;&#x597d; world
up
-1
Drake
14 years ago
Decode Hexa to Strings =)
<?php
class PhpHex2Str
{
private
$strings;

private 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}#', 'x_hex2str', $this->strings);
}
}

// Example
$strings = 'a %20 b%0A h %27 h %23';

$obj = new PhpHex2Str;
$strings = $obj->decode($strings);
var_dump($strings);
?>
up
-2
Anteaus
9 years ago
Be aware that as of php5.4 you MUST NOT pass variables by reference, as in '[, int &$count ]' - if you do it will result in a fatal error.
I think the writer is trying to say that the function accepts the parameter by reference, but that is not how it reads. -Manual needs updating/clarifying ?
up
-3
alex dot cs00 at yahoo dot ca
14 years ago
Don't use this function to fetch BBCode, as explained. If you have some text that runs over 5000 chars (average), it will run out of its limit and makes you download the PHP page.

According to this, you should instead use something more advanced yet complex. You will need a function called "str_replace_once()" (search for it), one called "countWord()", the famous "after()", "before()", "between()".

str_replace_once does same as str_replace, but only replace first occurence. As for countWord, I guess you know how to count the number of a word occurence. As for after, before and between, this is a function that you may find easily somewhere on the site by a user. Else, you can do it.

The following function is able to do all blocks, supposing [code] and [/code], you might wish things between parents dont get parsed, including [code] if inside of another [code].

<?php
function prepareCode($code, $op, $end)
{
$ix = 0;
$iy = 0;
$nbr_Op = countWord($op, $code);
while(
$ix < $nbr_Op)
{
if(
in_string($op, before($end, $code), false))
{
// The following piece of code replace the default [tag] by [tag:#]
$code = str_replace_once($op, substr($op, 0, -1).':'.$ix.']', $code);
$iy++;
}
elseif(
in_string($end, before($op, $code), false))
{
$iy = $iy-1;
$code = str_replace_once($end, substr($end, 0, -1).':'.($ix-1).']', $code);
$ix = $ix-2;
}
$ix++;
}
while(
in_string($end, $code))
{
$code = str_replace_once($end, substr($end, 0, -1).':'.($iy-1).']', $code);
$iy=$iy-1;
}

$code = preg_replace('#\\'.substr($end, 0, 1).':-[0-9]\]#i', '', $code);
if(
in_string(substr($op, 0, -1).':0]', $code) && !in_string(substr($end, 0, -1).':0]', $code))
{
$code .= substr($end, 0, -1).":0]";
}
return
$code;
}
?>

$code returns the whole text semi-formated. You only need to use it as :
$code = prepareCode($code="Your text", $op="[tag]" , $end="[/tag]");
Then just replace the parent tags :
str_replace("[tag:0]", "<tag>", $code);
str_replace("[/tag:0]", "</tag>", $code);
So at the end something like :
[
up
-1
jobowo
2 years ago
Note that when using the 'Use ($variable)' with preg_replace_callback, if you wish the value to be altered by the anonymous function, you must pass the value in by reference. eg preg_replace_callback($pattern, function($matches) use (&$alterThis) { $alterThis+=$something;},$string);
up
-5
Underdog
10 years ago
For the callback I advise only to use a permanent or anonymous function.

Depending on the usage you may encounter memory issues when using create_function for the callback possibly due to attempts at being compatible with PHP 5.2 or prior. Some servers refuse to update their PHP for whatever reason.

Please peruse the create_function documentation for more details regarding its memory usage.

Regards.
up
-3
tgage at nobigfoot dot com
6 years ago
To use variable from the parent scope of the anonymous callback function passed to preg_replace_callback(), utilize the use() parameter.

$var1 = "one";
$var2 = "two";
$line = preg_replace_callback('/^.*$/',
function( $matches ) use ( $var1, $var2 ) {
return( $var1 . " " . $var2 );
}, $line);

Will replace the entire string with the concatenated values or $var1 and $var2 ("one two") from the parent scope.
To Top