What is the function of str_replace in resolving the issue of displaying spaces in PHP?

When displaying spaces in PHP, the spaces may not be visible as HTML collapses consecutive spaces into a single space. To resolve this issue, we can use the str_replace function in PHP to replace spaces with the HTML entity for non-breaking space ( ). This will ensure that all spaces are displayed as intended on the webpage.

<?php
$string_with_spaces = "Hello     World";
$string_with_spaces_replaced = str_replace(" ", " ", $string_with_spaces);
echo $string_with_spaces_replaced;
?>