Page 1 of 1

[resolved]\n and \r are adding spaces with echo

Posted: Fri Oct 03, 2008 7:37 pm
by MALLON

Code: Select all

 
    function colorizeChar($char2){
        $num = mt_rand(0, 16777215);
        $num = dechex($num);
        echo "<font color = " . $num . ">" . $char2 . "</font>\n";
    }
 
My code goes through an HTML page and colorizes all text to some random color. It works just fine. The problem is when I try to format the source code so it's not messy by adding a newline after every colored character.

For some reason, it's putting a space in between every character too. You can see what I mean here:

http://xyzcrew.info/test.php

If I leave out the \n, no spaces and the source is unformatted. It also behaves the same way with \r. I've tried putting \n and \r in their own echos, using print(), etc. Nothing seems to solve it. This glitch (?) is happening with FF3 and IE7.

Thanks,

--MALON

Re: \n and \r are adding spaces with echo

Posted: Sat Oct 04, 2008 7:47 pm
by MALLON
Sorry for the bump. I'll only do one, just in case there really is an explanation. If no one responds, I'll forget about it and assume it's a bug.

Thanks,

--MALON

Re: \n and \r are adding spaces with echo

Posted: Sat Oct 04, 2008 8:51 pm
by yacahuma
could you post the whole code?

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 2:33 pm
by MALLON

Code: Select all

 
<?php
    $file=fopen("hi.7bx","r") or exit("Unable to open file!");
    $tag;
    $char;
    $inTag;
    $skip;
    while (!feof($file))
    {
        $char .= fgets($file);
        if (strpos($char, "<body"))
            break;
    }
    
    echo $char;
    while (!feof($file))
    {
        $char = fgetc($file);
        
        if ($char == "<")
            $inTag = true;
        else if ($char == ">"){
            $inTag = false;
            echo $char;
            $skip = true;
        }
        
        if ($char == "\n" || $char == "\r")
            $skip = true;
                
        if(!$skip){
            if ($inTag)
                echo $char;
            else
                colorizeChar($char);
        }else
            $skip = false;
    }
    
    function colorizeChar($char2){
        $num = mt_rand(0, 16777215);
        $num = dechex($num);
        echo "<font color = " . $num . ">" . $char2 . "</font>\n";
    }
    fclose($file);
?>
 

I made 2 versions of the page, with \n and without \n.

http://xyzcrew.info/test.php is exactly the code above and
http://xyzcrew.info/test2.php is exactly the code above with removed \n from the echo line in the colorizeChar function.

--MALON

P.S. If you're wondering about what a 7bx file is (because that's the file I'm opening), it's just a regular old text file with a renamed extension so that I could upload it in binary rather than ASCII. FileZilla automatically uploads recognized file extensions as ASCII. I didn't want the server OS (or my OS for some reason) to add in some strange characters, so I made it a crazy extension.

P.P.S. I'm using PHP 5.2.5

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 3:34 pm
by VladSun
1. "bla-bla \n\r"
2. use corect HYML syntax:

Code: Select all

function colorizeChar($char2){
        $num = mt_rand(0, 16777215);
        $num = dechex($num);
        echo "<font color = \"" . $num . "\">" . $char2 . "</font>\n";
    }
3. remove white spaces from your $char:

Code: Select all

function colorizeChar($char2){
        $num = mt_rand(0, 16777215);
        $num = dechex($num);
        echo "<font color = \"" . $num . "\">" . trim($char2) . "</font>\n";
    }

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 3:36 pm
by MALLON
Yes, every font is on a new line, but the resulting web page add spaces between the letters for some reason, that's the problem. Just look at the difference between http://www.xyzcrew.info/test.php and http://www.xyzcrew.info/test2.php

The only difference is that one prints \n and the other doesn't.

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 3:39 pm
by VladSun
I've just edited my post. Sorry - i didn't read carefully :)

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 3:45 pm
by MALLON
k, i've updated the code with this function:

Code: Select all

 
    function colorizeChar($char2){
        $num = mt_rand(0, 16777215);
        $num = dechex($num);
        echo "<font color = \"" . $num . "\">" . trim($char2) . "</font>\n\r";
    }
 

You can see the results here: http://www.xyzcrew.info/test3.php

I was hoping you had found the problem, but it appears the error remains.

:(

Thanks for trying :)

--MALON

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 4:00 pm
by VladSun
So, it appears that the new line character is processed as a single white space by the HTML renders (which, I believe, should be this way) - i.e. you can not do anything about it, except putting all FONT tags on a single line ...

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 4:07 pm
by MALLON
But that doesn't make sense to me, because if you wrote HTML code by hand, you'd have lots of linefeed (and carriage return if you're on windows) characters everywhere, making spaces appear every time you hit return in the source to go to a new line. No?

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 4:19 pm
by VladSun

Code: Select all

<html><body>abab</body></html>
It's correct only for block HTML elements, because they are followed by a "hidden" <br /> and the space after them has no effect. For inline HTML elements every white space character or series of is considered a single space.

Re: \n and \r are adding spaces with echo

Posted: Sun Oct 05, 2008 4:25 pm
by MALLON
Ok, thank you. I guess my situation is resolved.