SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,531
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Where are my spaces going?

    I have a form with $_POST['firstName']

    Why is it that if I type in the field "<space>Debbie<15spaces>" and then echo the field value with PHP, I only see...

    Code:
    echo "untrimmed: firstName='" . $_POST['firstName'] . "'<br />";
    ' Debbie '

    Sincerely,


    Debbie

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    html collapses white space

    if you need to see them, use the PRE tag
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,531
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    html collapses white space

    if you need to see them, use the PRE tag
    I was playing around with this PHP code to understand how it works and can maybe make my code simpler...

    PHP Code:
    // Trim all form data.
    $trimmed array_map('trim'$_POST);

    echo 
    "untrimmed: firstName='" $_POST['firstName'] . "'<br />";
    echo 
    "trimmed: firstName='" $trimmed['firstName'] . "'<br />"
    I figured if I entered a whole bunches of spaces around my name in the form it would help demonstrate that it was working.


    Debbie

  4. #4
    Resident curmudgeon bronze trophy gary.turner's Avatar
    Join Date
    Jan 2009
    Location
    Dallas
    Posts
    990
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Non-breaking spaces don't collapse.

    cheers,

    gary
    Anyone can build a usable website. It takes a graphic
    designer to make it slow, confusing, and painful to use.

    Simple minded html & css demos and tutorials

  5. #5
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    I was playing around with this PHP code to understand how it works and can maybe make my code simpler...
    You want simpler, STOP USING DOUBLE QUOTES ON PHP STRINGS!

    ... and you can get the spaces to show with a hint of CSS without the container going block-level using white-space: pre, though really PRE by itself should be enough to do what you want.

    Code:
    echo '
    <pre>
    untrimmed: firstName = "',$_POST['firstName'],'"
    trimmed: firstName = "',$trimmed['firstName'],'"
    </pre>';
    Using single quotes also means white space in the output is preserved, and it only needs ONE echo statement... and the presence of <pre> means your carriage returns show up on the page.

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,531
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    You want simpler, STOP USING DOUBLE QUOTES ON PHP STRINGS!

    ... and you can get the spaces to show with a hint of CSS without the container going block-level using white-space: pre, though really PRE by itself should be enough to do what you want.

    Code:
    echo '
    <pre>
    untrimmed: firstName = "',$_POST['firstName'],'"
    trimmed: firstName = "',$trimmed['firstName'],'"
    </pre>';
    Using single quotes also means white space in the output is preserved, and it only needs ONE echo statement... and the presence of <pre> means your carriage returns show up on the page.
    Okay, thanks.


    Debbie


Bookmarks

Posting Permissions

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