Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Insert Multiple Spaces

I've got some data that needs to be cleaned up into a fixed length format. I'm using PHP to grab the data out, covert it, and put it back in, but it's not working as planned. There is a certain point in each piece in the middle of the data where there should be spaces to increase the length to the proper amount of characters. The code I'm using to do this is:

while ($row = mysql_fetch_array($databasetable)) {
    $key = $row['KEY'];
    $strlength = strlen($key);
    while ($strlength < 33) {
        $array = explode(' TA',$key);
        $key = $array[0] . '  TA' . $array[1];
        $strlength++;
    }
}

It's taking a ' TA' and adding two spaces before it, rinse and repeat until the total length is 33, however when I output the value, it just returns a single space. Funny part is that even though it is displaying a single space, it returns a strlen of 33 even if it's not displaying 33 characters.

Any help in figuring this out would be greatly appreciated.

like image 378
bmbaeb Avatar asked Dec 27 '22 22:12

bmbaeb


2 Answers

HTML will have extra spaces filtered out.

To force extra spaces to be shown, use '&nbsp;' rather than ' '.

like image 80
tvkanters Avatar answered Jan 05 '23 00:01

tvkanters


@TVK- is correct, HTML ignores multiple-space whitespace - it'll turn it into one space.

In addition to his solution, you can use the CSS instruction white-space: pre to preserve spaces.

like image 20
ceejayoz Avatar answered Jan 04 '23 22:01

ceejayoz