The "%-22.22s" will add spaces to the names up to 22 characters (which is why I used 22 characters for each name in my test HTML). This should be changed to "%.22s" so that all names will be truncated to 22 characters if they have more than 22 characters. Change the 22 as needed. Once you fix this, you can remove that replace while loop entirely.
Using "%-22.22s" would be required if you weren't using HTML but instead using just plain text (with a fixed-width font) to create column alignment.