How can PHP functions like str_replace() be utilized to address data formatting issues, such as replacing spaces with specific characters?

Data formatting issues, such as replacing spaces with specific characters, can be addressed using PHP functions like str_replace(). This function allows you to search for a specific substring within a string and replace it with another substring. By using str_replace(), you can easily replace spaces with any desired character, helping you format your data as needed.

// Replace spaces with an underscore in a string
$string = "This is a sample string with spaces";
$new_string = str_replace(" ", "_", $string);
echo $new_string;