www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: undesirable linebreak after heredoc

  1. #1
    Join Date
    Jul 2006
    Posts
    373

    undesirable linebreak after heredoc

    Hi,

    i have the following code

    PHP Code:
    $bye=<<<EOF
    <span>goodbye</span>
    EOF; 
    HTML Code:
         <td class="header_right">
           <span>hello</span>
            $bye 
         </td>
    which outputs:

    HTML Code:
         <td class="header_right">
           <span>hello</span>
           <span>goodbye</span>
    
         </td>
    as you may see, there is a double line break between the last </span> and the </td> which should not be there. How can i take it out? Below there is a temporary solution:

    PHP Code:
         <td class="header_right">
           <
    span>hello</span
          
    $bye </td
    but this outputs something like this:

    HTML Code:
         <td class="header_right">
          <span>hello</span>
         <span>goodbye</span>
    </td>

    which doesnt look good either, since the last </td> wont align with the rest of the code. If i give some extra tabs between $bye and </td> like this

    PHP Code:
         <td class="header_right">
           <
    span>hello</span$bye     </td
    then i get what i want but it just doesnt feel right. there must be a cleaner way to do it. Isnt?

    Thank you.

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    What's the difference? It doesn't matter what spaces are there.

    Why is the extra spacing bothering you?

  3. #3
    Join Date
    Jul 2006
    Posts
    373
    Because it doesnt look good in the source code. Im quite picky when it comes to presentation and that space just doesnt look right. I know many web developers dont care about how their code look but i do. I want it to comply with HTML tidy standards.

  4. #4
    Join Date
    Dec 2011
    Posts
    34
    Read up on the trim function
    the reference manual is an invaluable source of information, which reminds me of a neat extension I picked up for chrome, PHP Ninja Very useful tool

  5. #5
    Join Date
    Jul 2006
    Posts
    373
    Thanks. I know the trim function but i think i fail to figure out how can use it so i can remove only and exactly the last break of the text. Actually i still dont understand why do i get that break since there is nothing between the last line of code and the EOF;

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Spaces/Tabs/Line breaks and tidy html in general is a waste of bandwidth.
    I personally prefer to strip everything unnecessary. Just make sure the code you write is clean.

    Sorry if that doesn't help your current problem, but I gave up making the output looking pretty a long long time ago. I found that under different circumstances you needed some html to be more indented than in other places, what then?

  7. #7
    Join Date
    Jul 2006
    Posts
    373
    I respect your preference and i know what you mean, but having a clean and readable code makes it easy to edit. Thats why i want it to comply with tidy standards.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    If it's important enough to you that you don't mind a few milliseconds of extra processing: http://www.charles-reace.com/blog/20...put-with-tidy/
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    I guess you could do this:

    PHP Code:
    <?php 
    echo trim(<<<EOF
    <span>goodbye</span>
    EOF
    );
    That's the closest I could come up with to removing the tailing \n. Although it does not match your example, I'm just showing the syntax.

    Ran it with PHP 5.3.5-1ubuntu7.4 with Suhosin-Patch (cli) (built: Dec 13 2011 18:25:14)

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    Heredoc does some funky stuff with white spaces sometimes... Might be better to rethink what you have going on and use the more standard variable declaration using quotes and all...

  11. #11
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Have you tried...
    PHP Code:
    $bye=<<<EOF 
    <span>goodbye</span>EOF

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    Quote Originally Posted by MrCoder View Post
    Have you tried...
    PHP Code:
    $bye=<<<EOF 
    <span>goodbye</span>EOF
    The "EOF;" (or whatever string you choose) must be the only thing on its line (including no leading white-space).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  13. #13
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Quote Originally Posted by NogDog View Post
    The "EOF;" (or whatever string you choose) must be the only thing on its line (including no leading white-space).
    Was worth a shot

  14. #14
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    you did see my example correct?

    The "EOF" MUST be on it's own line, the semicolon can be 1000 lines down and not exclusively separated by \r, \n, and spaces. I put a ) there to close trim.

    It does run...

  15. #15
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Quote Originally Posted by eval(BadCode) View Post
    you did see my example correct?

    The "EOF" MUST be on it's own line, the semicolon can be 1000 lines down and not exclusively separated by \r, \n, and spaces. I put a ) there to close trim.

    It does run...
    I would only recommend on top of that to use rtrim instead, just incase leading whitespaces or tabs were required for formatting.

    Modified from eval(BadCode) previous submission:

    PHP Code:
    <?php

    $bye 
    rtrim(<<<EOF
    <span>goodbye</span>
    EOF
    );

    echo 
    $bye;

    ?>
    Tested.
    Last edited by bionoid; 01-10-2012 at 06:27 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles