Skip to content
PHP Logo

How can str_replace() be used to replace placeholders in PHP files?

To replace placeholders in PHP files, you can use the str_replace() function. This function takes three parameters: the search string, the replacement string, and the input string. By using this function, you can easily replace placeholders in PHP files with the desired content.

php
<?php
// Define placeholders and their corresponding values
$placeholders = array(
    '{NAME}' => 'John Doe',
    '{AGE}' => '25',
);

// Read the content of the PHP file
$file_content = file_get_contents('example.php');

// Replace the placeholders with their values
$file_content = str_replace(array_keys($placeholders), array_values($placeholders), $file_content);

// Output the modified content
echo $file_content;
?>