How can one continuously replace all instances of double spaces in a string until no change occurs in its length in PHP?

To continuously replace all instances of double spaces in a string until no change occurs in its length in PHP, you can use a while loop that checks if the length of the string changes after replacing double spaces with single spaces. If the length remains the same, it means no more double spaces exist in the string.

$string = "This  is  a  string  with  double  spaces";
while (strlen($string) != strlen(str_replace("  ", " ", $string))) {
    $string = str_replace("  ", " ", $string);
}
echo $string;