How can conditional statements be utilized in PHP to filter out specific characters, such as spaces, when manipulating strings for colorization?

When manipulating strings for colorization in PHP, it may be necessary to filter out specific characters, such as spaces, to ensure the desired output. Conditional statements can be utilized to check each character in the string and exclude any unwanted characters before applying colorization.

<?php
$string = "Hello, World!";
$colorizedString = "";

for ($i = 0; $i < strlen($string); $i++) {
    if ($string[$i] != " ") {
        $colorizedString .= "<span style='color: red;'>".$string[$i]."</span>";
    } else {
        $colorizedString .= $string[$i];
    }
}

echo $colorizedString;
?>