How can PHP scripts be modified to handle string replacements in multiple pages efficiently?
To efficiently handle string replacements in multiple pages in PHP scripts, you can create a function that accepts the string to be replaced, the replacement string, and the content of the page. This function can use the str_replace() function to replace the specified string in the content. By encapsulating this logic in a function, you can easily apply the replacements to multiple pages without duplicating code.
function replaceStringInPage($search, $replace, $content) {
return str_replace($search, $replace, $content);
}
// Example of how to use the function
$search = 'old_string';
$replace = 'new_string';
$page_content = file_get_contents('example_page.html');
$updated_content = replaceStringInPage($search, $replace, $page_content);
// Output the updated content
echo $updated_content;