Current Location: Home> Latest Articles> Debug regular callback logic with var_dump and print_r

Debug regular callback logic with var_dump and print_r

M66 2025-05-18

When using preg_replace_callback_array to handle complex text replacement, it is often necessary to debug matches in regular callback functions and determine whether the logic is correct. PHP provides two very practical debugging functions: var_dump() and print_r() . This article will demonstrate through an example how to debug regular callback logic with the help of these two functions in preg_replace_callback_array .

What is preg_replace_callback_array ?

preg_replace_callback_array() is a function provided by PHP 7.0 and above. It allows you to pass in an associative array composed of regular expressions and callback functions. It will traverse the matching text and replace it according to the corresponding callback functions.

 preg_replace_callback_array(array $patterns_and_callbacks, string $subject, int $limit = -1, int &$count = null): string

Compared with preg_replace_callback , it can handle multiple regular expressions and multiple callback functions, with clearer logic and stronger scalability.

Practical case: Debugging multi-mode replacement logic

Suppose we have a piece of text that mixes Markdown-style links and custom {{code}} tags, and we want to use preg_replace_callback_array to handle them uniformly:

 $text = 'Click [here](https://m66.net) check the details,Or use {{print_name}} variable。';

We define two patterns and specify their respective callback functions:

 $patterns = [
    // match markdown Link:[text](URL)
    '/\[(.*?)\]\((.*?)\)/' => function ($matches) {
        echo "match到 markdown Link:\n";
        print_r($matches);
        return '<a href="' . htmlspecialchars($matches[2]) . '">' . htmlspecialchars($matches[1]) . '</a>';
    },
    // match {{variable}} form
    '/\{\{(.*?)\}\}/' => function ($matches) {
        echo "match到variable标签:\n";
        var_dump($matches);
        $variableName = $matches[1];
        // 模拟variable替换
        $variables = [
            'print_name' => 'username'
        ];
        return isset($variables[$variableName]) ? $variables[$variableName] : '';
    }
];

$result = preg_replace_callback_array($patterns, $text);
echo "\nFinal replacement result:\n" . $result;

Example output result:

 match到 markdown Link:
Array
(
    [0] => [here](https://m66.net)
    [1] => here
    [2] => https://m66.net
)
match到variable标签:
array(2) {
  [0]=>
  string(17) "{{print_name}}"
  [1]=>
  string(10) "print_name"
}

Final replacement result:
Click <a href="https://m66.net">here</a> check the details,Or use username variable。

Recommendations for use

  • Use print_r() to output arrays with clear structures, which is more suitable for human reading;

  • Using var_dump() to view data types and lengths is especially useful when dealing with multiple data structures;

  • In order to avoid debugging information contaminating web page output, it is recommended to debug in the CLI or log file;

  • Remove or comment all debug statements before going online to ensure the output is neat.

Summarize

preg_replace_callback_array provides powerful multi-mode regular replacement capabilities. In the debugging stage, combined with var_dump() and print_r() can help us quickly understand the matching situation and verify whether the logic is correct. Mastering this debugging method can greatly improve your efficiency in handling complex text processing tasks.