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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MALLON
Forum Newbie
Posts: 10
Joined: Mon Sep 29, 2008 9:48 pm

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

Post 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
Last edited by MALLON on Sun Oct 05, 2008 4:25 pm, edited 1 time in total.
MALLON
Forum Newbie
Posts: 10
Joined: Mon Sep 29, 2008 9:48 pm

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

Post 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
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

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

Post by yacahuma »

could you post the whole code?
MALLON
Forum Newbie
Posts: 10
Joined: Mon Sep 29, 2008 9:48 pm

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

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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";
    }
Last edited by VladSun on Sun Oct 05, 2008 3:38 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
MALLON
Forum Newbie
Posts: 10
Joined: Mon Sep 29, 2008 9:48 pm

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

Post 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.
Last edited by MALLON on Sun Oct 05, 2008 3:40 pm, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

I've just edited my post. Sorry - i didn't read carefully :)
There are 10 types of people in this world, those who understand binary and those who don't
MALLON
Forum Newbie
Posts: 10
Joined: Mon Sep 29, 2008 9:48 pm

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

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
MALLON
Forum Newbie
Posts: 10
Joined: Mon Sep 29, 2008 9:48 pm

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

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
MALLON
Forum Newbie
Posts: 10
Joined: Mon Sep 29, 2008 9:48 pm

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

Post by MALLON »

Ok, thank you. I guess my situation is resolved.
Post Reply