In PHP, preg_replace_callback is a highly useful regex replacement function that allows you to process matched results using a callback function. It is often used for complex replacement needs, especially when matched content requires dynamic modification. Understanding the parameters of the callback function is crucial for correctly using this function. This article will explain the role of each parameter in the preg_replace_callback callback function step by step.
The preg_replace_callback function executes a callback function to replace parts that match a regular expression. Its basic syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">preg_replace_callback</span></span><span>(</span><span><span class="hljs-variable">$pattern</span></span><span>, </span><span><span class="hljs-variable">$callback</span></span>, </span><span><span class="hljs-variable">$subject</span></span><span>);
</span></span>
$pattern: The regular expression pattern.
$callback: The callback function that processes the matched results.
$subject: The string on which matching and replacement are performed.
The callback function is the core of preg_replace_callback. Typically, when the matched content requires special handling, we use the callback function to manipulate each match. The callback can be defined as an anonymous function or a named function. Regardless of the form, the callback function receives a specific set of parameters.
The first parameter of the callback function is an array containing all results matched by the regular expression. It includes:
The first element ($matches[0]) is the entire matched string.
The subsequent elements ($matches[1], $matches[2], ...) are the captured groups (the parenthesis portions of the regex) results.
For example, assume we have the following regex:
<span><span><span class="hljs-variable">$pattern</span></span><span> = </span><span><span class="hljs-string">'/(\d+)\-(\d+)/'</span></span><span>;
</span><span><span class="hljs-variable">$subject</span></span><span> = </span><span><span class="hljs-string">'12-34, 56-78'</span></span><span>;
</span></span>
Then, using preg_replace_callback to process:
<span><span><span class="hljs-title function_ invoke__">preg_replace_callback</span></span><span>(</span><span><span class="hljs-variable">$pattern</span></span><span>, function(</span><span><span class="hljs-variable">$matches</span></span><span>) {
</span><span><span class="hljs-title function_ invoke__">print_r</span></span><span>(</span><span><span class="hljs-variable">$matches</span></span><span>);
}, </span><span><span class="hljs-variable">$subject</span></span><span>);
</span></span>
The output is:
<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
[</span><span><span class="hljs-number">0</span></span><span>] => </span><span><span class="hljs-number">12</span></span>-</span><span><span class="hljs-number">34</span></span>
[</span><span><span class="hljs-number">1</span></span><span>] => </span><span><span class="hljs-number">12</span></span>
[</span><span><span class="hljs-number">2</span></span><span>] => </span><span><span class="hljs-number">34</span></span>
)
</span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
[</span><span><span class="hljs-number">0</span></span><span>] => </span><span><span class="hljs-number">56</span></span>-</span><span><span class="hljs-number">78</span></span>
[</span><span><span class="hljs-number">1</span></span><span>] => </span><span><span class="hljs-number">56</span></span>
[</span><span><span class="hljs-number">2</span></span><span>] => </span><span><span class="hljs-number">78</span></span>
)
</span></span>
$matches[0] is the complete matched string.
$matches[1] and $matches[2] are the matched subpattern parts.
The return value of the callback function becomes the replacement result. This means that the callback is not just a data processing function; its return value directly affects what is replaced. If the callback returns a string, that string replaces the originally matched portion.
For example:
<span><span><span class="hljs-variable">$pattern</span></span><span> = </span><span><span class="hljs-string">'/(\d+)\-(\d+)/'</span></span><span>;
</span><span><span class="hljs-variable">$subject</span></span><span> = </span><span><span class="hljs-string">'12-34, 56-78'</span></span><span>;
<p></span>$replaced = preg_replace_callback($pattern, function($matches) {<br>
// Use the sum of the two numbers as the replacement<br>
return $matches[1] + $matches[2];<br>
}, $subject);</p>
<p>echo $replaced;<br>
</span>
The output is:
<span><span>46, 134
</span></span>
In this example, we add the matched numbers together and replace the original matched string with the result.
The callback function can also access additional information through $offset and $limit parameters. These are typically handled internally by preg_replace_callback, but you can control them more flexibly using preg_replace_callback_array. In most common scenarios, the first parameter ($matches) and the return value of the callback are sufficient.
preg_replace_callback and its callback function offer great flexibility, allowing developers to dynamically replace content based on matches. The first parameter $matches provides detailed information about matches, including the full match and captured groups, while the return value directly determines the replaced content. With these fundamental concepts mastered, you can use preg_replace_callback to perform complex regex replacement operations.