php echo add extra space

1.4k Views Asked by At

Input text :

Test one two three fourfive

Text is splitted by preg_split with additional processing and now result array is:

Test one
<span>two</span>
three
<span>four</span>
five

When I echo this array in loop I get this:

Test one two three four five

Four and five should be displayed together, without space.

In HTML source it looks like so:

Test one
<span>two</span>
three
<span>four</span>
five

Extra space is added after four.

When I directly write this in HTML

Test one <span>two</span> three <span>four</span>five

text is echoed correctly. It seems that new line in HTML add extra space. Does someone know what happens here?

3

There are 3 best solutions below

2
On

A newline is a space. Remove that and your space disappears.

0
On

According to W3C, newlines are whitespace characters and should be rendered as a space by browsers.

If you want to get rid of the space, use <pre> and format your text as needed, or modify your markup so you can left-float "five."

0
On

Remove \n from your echo and it should be fine.

Just like OP suggest newline is whitespace too.