Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,473
    Thanks
    279
    Thanked 32 Times in 31 Posts

    preg_replace whitespace removal

    hi, im curious if i have the below code why is my string length 1 after POST if i enter 5 spaces. should it not be null


    //get rid of whitespace

    Code:
     
    $var['username'] = preg_replace('/\s\s+/', ' ', $var['username']);
    
          $stlen = strlen($var['username']);
    update: or am i just replacing it with itself basically..

    would it be better this way

    Code:
    $var['username'] = str_replace(' ', '', $var['username']);
    if someone enters any spaces i want them removed i can capture string length.
    Last edited by durangod; 12-06-2010 at 04:35 PM. Reason: update

  • #2
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,473
    Thanks
    279
    Thanked 32 Times in 31 Posts
    i got it i just tossed in a trim in front of the preg replace

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,473
    Thanks
    279
    Thanked 32 Times in 31 Posts
    ok i guess i need help on this it works accept for
    1s2s3s4s5ssssss

    (s) meaning space..
    it does not remove those internal spaces..

  • #4
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,473
    Thanks
    279
    Thanked 32 Times in 31 Posts
    ok i got it i used the string replace function and it passed all tests..


  •  

    Tags for this Thread

    Posting Permissions

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