Results 1 to 15 of 15
  1. #1

    PHP question about \t and \r

    I'm trying to make the html look descent when I view the source code for my current project but I'm having a bit of trouble with \t and \r.

    I'm creating an art gallery that will load 4 content boxes horizontally and then load a clear div. The code works properly it just doesn't look pretty when I view the source.

    The function to create the content boxes display the html code properly with the tabs and line breaks..
    PHP Code:
    echo "\t<div class=\"frame\">";
    echo 
    'content from the database';
    echo 
    "</div>\r"
    The div will load after every fourth content box but the \t and \r do not create a tab/line break in the html code...
    PHP Code:
    if ($i 3) {$i++;} else {
         echo 
    "\t\t<div style=\"clear:both;\"></div>\r";
         
    $i=0;

    The output:
    HTML Code:
        <div class="frame">content</div>
        <div class="frame">content</div>
        <div class="frame">content</div>
        <div class="frame">content</div>
    <div style="clear:both;"></div>  <div class="frame">content</div>
        <div class="frame">content</div>
        <div class="frame">content</div>
        <div class="frame">content</div>
    <div style="clear:both;"></div>  <div class="frame">content</div>
    What I'm trying to get the display to look like:
    HTML Code:
        <div class="frame">content</div>
        <div class="frame">content</div>
        <div class="frame">content</div>
        <div class="frame">content</div>
            <div style="clear:both;"></div>
        <div class="frame">content</div>
        <div class="frame">content</div>
        <div class="frame">content</div>
        <div class="frame">content</div>
            <div style="clear:both;"></div>  
        <div class="frame">content</div>
    I'm using WAMP with php5.2.8 and MySQL 5.1.30 if that matters. Does anyone have any suggestions?

    Thanks in advance!

  2. #2
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    Odd behaviour. The first thing I'd try is using a valid line ending, since \r isn't right for your system (only early Macs used it IIRC). On Windows you could use \r\n although I suspect \n might work too. For portability / best practice use the built-in constant PHP_EOL.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  3. #3
    Join Date
    Mar 2009
    Posts
    649
    You could also use Tidy.

    PHP Code:
    <?php
    ob_start
    ();
    echo 
    $messyHTMLCode;
    $html ob_get_clean();
    $tidy = new Tidy;
    $config = array('indent' => 'auto''output-xhtml' => true);
    $tidy->parseString($html$config);
    $tidy->cleanRepair();
    echo 
    $tidy;
    ?>
    [GB ≠ GiB] [MB ≠ MiB] [kB ≠ kiB] [1000 ≠ 1024] [Giga ≠ gram] [Mega ≠ milli] [Kelvin ≠ kilo] [Byte ≠ bit]
    There is no millibit. There is no gram-bit. There is no Kelvin-Byte.

  4. #4
    Join Date
    Feb 2008
    Location
    Richmond Hill, ON
    Posts
    24
    I think you want to use the character entities - try &tab; for tabs and &newline; (I think) for the new lines - though for html there's also <br /> for line breaks, why aren't you using that?

  5. #5
    Join Date
    Mar 2009
    Posts
    649
    Quote Originally Posted by Corazu View Post
    I think you want to use the character entities - try &tab; for tabs and &newline; (I think) for the new lines - though for html there's also <br /> for line breaks, why aren't you using that?
    Because he wants the source code formatted properly. Neither of your suggestions will do that.
    [GB ≠ GiB] [MB ≠ MiB] [kB ≠ kiB] [1000 ≠ 1024] [Giga ≠ gram] [Mega ≠ milli] [Kelvin ≠ kilo] [Byte ≠ bit]
    There is no millibit. There is no gram-bit. There is no Kelvin-Byte.

  6. #6
    Join Date
    Feb 2008
    Location
    Richmond Hill, ON
    Posts
    24
    Quote Originally Posted by petteyg359 View Post
    Because he wants the source code formatted properly. Neither of your suggestions will do that.
    Oh I see now what he means. Misread that.

    http://stackoverflow.com/questions/9...l-output-clean From Stack-Overflow has the same question, looks like he's using the same idea with tabs and new lines.

    You're using WAMP so your newline character is actually \r\n - though I think for the code, the browser will interpret \n regardless (most editors do anyway). The carriage return (\r) only applies as a newline character on Macs if I recall, the Windows version is technically \r\n (Carriage Return-Linefeed) but most windows editors pick up \n on it's own just fine. The linefeed by itself is unix/linux format.

    Try swapping to the \n and see if that works, as to the tabs..I'm not sure why they wouldn't work.

  7. #7

    Thanks

    I've tried using /t, /n, /r in different combination but it still won't work with wamp.

    The good news is...I've uploaded the files to two different servers and it worked just fine. I guess the version of wamp I'm using just doesn't like me.

    Thanks again for your help!

  8. #8
    Join Date
    Feb 2006
    Location
    Kusadasi, Turkey
    Posts
    3,273
    It will not work as expected on most cases. \r or \t are not rendered by HTML. You actually need to use them directly:

    PHP Code:
    if ($i 3) {$i++;} else {
        echo 
    "        <div style=\"clear:both;\"></div>
    "
    ;
        
    $i=0;

    You see how I added the tab and line break literally? If you want to make your HTML output look tidy, this is easiest the way. Harder way is to use chr() function to echo those characters, which is completely unnecessary.

    And also, never use double quotes for html output, it will be a hassle to escape the actual quotes in html. See the difference below. $image_source is the variable.

    PHP Code:
    echo "
    <div class=\"somediv\" style=\"clear:both;\" align=\"right\">
        <img src=\"
    $image_source\" alt=\"text\" width=\"300\" height=\"200\"/>
    </div>
    "


    versus this:

    PHP Code:
    echo '
    <div class="somediv" style="clear:both;" align="right">
        <img src="'
    .$image_source.'" alt="text" width="300" height="200"/>
    </div>
    '

    Fraud Record - Stop Fraud Clients, Report Abusive Customers.
    █ Combine your efforts to fight misbehaving clients.

    HarzemDesign - Highest quality, well designed and carefully coded hosting designs. Not cheap though.
    █ Large and awesome portfolio, just visit and see!

  9. #9
    Hello every one, nice discussion going on! Very productive. Can you guys help me in one thing. I am thinking to start learning PHP, whats best for me?

    Where should I start from?? I will be very thankful to you guys! If anyone can help me!

  10. #10
    Join Date
    Feb 2008
    Location
    Richmond Hill, ON
    Posts
    24
    Quote Originally Posted by Sheikh Ahsan View Post
    Hello every one, nice discussion going on! Very productive. Can you guys help me in one thing. I am thinking to start learning PHP, whats best for me?

    Where should I start from?? I will be very thankful to you guys! If anyone can help me!
    You shouldn't be Hijacking a thread like this. Try googling for PHP Tutorials; PHP.net (the manual) is also very helpful as well.

  11. #11
    Join Date
    Mar 2009
    Posts
    649
    Quote Originally Posted by Harzem View Post
    It will not work as expected on most cases. \r or \t are not rendered by HTML.
    Of course they aren't. They're not part of the HTML. They're part of what the PHP echo outputs. A literal tab inside of a string is output the same way as a \t inside a double-quoted string. Whether that output is parsed by an HTML browser or not makes no difference in what the echo statement actually outputs.
    [GB ≠ GiB] [MB ≠ MiB] [kB ≠ kiB] [1000 ≠ 1024] [Giga ≠ gram] [Mega ≠ milli] [Kelvin ≠ kilo] [Byte ≠ bit]
    There is no millibit. There is no gram-bit. There is no Kelvin-Byte.

  12. #12
    Join Date
    Feb 2006
    Location
    Kusadasi, Turkey
    Posts
    3,273
    Quote Originally Posted by petteyg359 View Post
    Of course they aren't. They're not part of the HTML. They're part of what the PHP echo outputs. A literal tab inside of a string is output the same way as a \t inside a double-quoted string. Whether that output is parsed by an HTML browser or not makes no difference in what the echo statement actually outputs.
    Sorry you are right, I'm used to use echo with single quotes, I didn't notice \r and \t would be sent as actual characters in html, which pretty much becomes the same thing as I recommended.
    Fraud Record - Stop Fraud Clients, Report Abusive Customers.
    █ Combine your efforts to fight misbehaving clients.

    HarzemDesign - Highest quality, well designed and carefully coded hosting designs. Not cheap though.
    █ Large and awesome portfolio, just visit and see!

  13. #13
    On windows platform, you have to use \r\n instead of \r or \n.
    Website Hosting by Rackset
    Professional, Affordable, Reliable

    Web Hosting Provider

  14. #14
    Join Date
    Mar 2005
    Location
    Athens, Greece
    Posts
    1,528
    I always use \n and it works.
    Antonis Adamakos @ FuzzFree :: Fully Managed Web Hosting, Development, Online Marketing

  15. #15
    Join Date
    Dec 2002
    Location
    Jackson, MI
    Posts
    1,526
    Yeah the windows return character is \r\n and \n will work on the rest. I always use \r\n just to be sure it works on whatever, because while linux/mac/etc will read /r/n just fine, windows will not display \n properly.

Similar Threads

  1. A Question of Space & Price - a big question!
    By active8 in forum Running a Web Hosting Business
    Replies: 7
    Last Post: 05-18-2006, 05:55 PM
  2. DNS MX Question : Multiple choice Question
    By nipl in forum Hosting Security and Technology
    Replies: 29
    Last Post: 11-17-2003, 01:02 PM
  3. Replies: 10
    Last Post: 05-08-2003, 08:28 AM
  4. wisehosting.net question (and a phpBB question)
    By Zero Enna in forum Web Hosting
    Replies: 29
    Last Post: 04-05-2002, 01:33 PM
  5. Email question/Host question
    By mjmsr in forum Web Hosting
    Replies: 2
    Last Post: 09-07-2001, 01:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •