What is the purpose of using str_replace in a PHP script when retrieving data from a MySQL database?

When retrieving data from a MySQL database in a PHP script, you may encounter special characters or unwanted strings that need to be replaced or removed. Using the str_replace function in PHP allows you to easily search for specific strings and replace them with another value. This can help sanitize and format the data before displaying it on a webpage, ensuring that it is safe and properly formatted for the user.

// Retrieve data from MySQL database
$data = $row['column_name'];

// Replace unwanted characters or strings
$data = str_replace('unwanted_string', 'replacement_string', $data);

// Display the sanitized data
echo $data;