What are some best practices for replacing placeholders in PDF documents with form data using str_replace in PHP to avoid errors?

When replacing placeholders in PDF documents with form data using str_replace in PHP, it is important to ensure that the placeholders are unique and distinct to avoid unintended replacements. One way to achieve this is by using a specific format for the placeholders, such as {{placeholder}}. Additionally, it is recommended to use a loop to iterate through the form data and replace each placeholder individually to prevent errors.

// Sample code for replacing placeholders in a PDF document with form data
$pdfContent = "Dear {{name}}, Thank you for your interest in {{product}}. Your order number is {{order_number}}.";

$formData = [
    'name' => 'John Doe',
    'product' => 'Product ABC',
    'order_number' => '12345'
];

foreach ($formData as $key => $value) {
    $placeholder = '{{' . $key . '}}';
    $pdfContent = str_replace($placeholder, $value, $pdfContent);
}

echo $pdfContent;