What alternative approach can be used to avoid the problem of nested replacements in the code?

The issue of nested replacements in code can be avoided by using a different approach such as using placeholders or tokens to mark the positions where replacements need to be made. By replacing these tokens with the actual values in a separate step, we can prevent the need for nested replacements and make the code cleaner and more efficient.

// Using placeholders to avoid nested replacements
$text = "Hello {name}, your order number is {order_number}.";
$replacements = [
    '{name}' => 'John Doe',
    '{order_number}' => '12345'
];

foreach ($replacements as $placeholder => $value) {
    $text = str_replace($placeholder, $value, $text);
}

echo $text;