PHP Insert Multiple Spaces
Asked Answered
C

8

5

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.

Caption answered 1/4, 2011 at 15:40 Comment(0)
V
4

@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.

Vague answered 1/4, 2011 at 15:45 Comment(1)
Thanks for the input. When I put in white-space: pre; it worked perfectly.Caption
R
6

HTML will have extra spaces filtered out.

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

Reniti answered 1/4, 2011 at 15:43 Comment(1)
Thanks for the input, I forgot that it was something HTML does because I haven't done much with that lately.Caption
V
4

@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.

Vague answered 1/4, 2011 at 15:45 Comment(1)
Thanks for the input. When I put in white-space: pre; it worked perfectly.Caption
M
3

Remember that, when doing an output to a webbrowser, it'll interpret it as HTML ; and, in HTML, several blank spaces are displayed as one.

A possibility would be to use the var_dump() function, especially if coupled with the Xdebug extension, to get a better idea of your data -- or to display it as text, sending a text-related content-type.


BTW : if you want to make sure a string contains a certain amount of characters, you'll probably want to take a look at str_pad()

Murillo answered 1/4, 2011 at 15:45 Comment(0)
B
1

Easiest options for you I think are

  • Wrap your output in <pre> tags
  • replace each space with &nbsp;
Blatherskite answered 1/4, 2011 at 15:47 Comment(0)
M
0

If you're rendering HTML, consecutive spaces will be ignored. If you want to force rendering of these, try using &nbsp;

Generally using multiple non breakable spaces one after another is a bad idea and might not bring a desired result unless you're using a monospaced font. If you want to move some piece of text to a certain position on your page, consider using margins

Marchand answered 1/4, 2011 at 15:47 Comment(0)
A
0

You can tell the browser that the text is preformatted

  this text will be displayed as it is formatted
     spaces should all appear.
         new lines will also be apparent
Assessor answered 1/4, 2011 at 16:12 Comment(0)
T
-1

Have you looked into str_pad? something like :

str_pad ( 'mystring' , 33 , '  TA' , STR_PAD_LEFT );
Thirion answered 1/4, 2011 at 15:44 Comment(1)
Yeah I see that now, its been a long day.Thirion
S
-1

I thing you can use str_repeat

echo str_repeat("&nbsp;", 15);
Spurge answered 16/8, 2020 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.