What are the potential pitfalls of using preg_replace for text replacement?

One potential pitfall of using preg_replace for text replacement is that it can be vulnerable to injection attacks if user input is not properly sanitized. To solve this issue, it is important to use the preg_replace_callback function instead, which allows for a callback function to be used for replacement.

// Example of using preg_replace_callback to safely replace text
$text = "Hello, [user]!";
$safe_text = preg_replace_callback('/\[user\]/', function($matches) {
    return "John Doe";
}, $text);

echo $safe_text;