Closed Thread
Results 1 to 7 of 7

Thread: strip white space

  1. #1
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    strip white space

    I need a function that strips white space from a string for my registration page. php_strip_whitespace() only works with 5.1 and newer so that won't work. I'm using trim() already but that only removes it from the ends. php.net isn't showing anything else when i run a search.

    any sugestions?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: strip white space

    Code:
    str_replace(" """$string); 

  4. #3
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    Re: strip white space

    thanks

  5. #4
    mikelbring is offline Programmer
    Join Date
    Jul 2008
    Location
    Nebraska
    Posts
    118
    Rep Power
    0

    Re: strip white space

    Quote Originally Posted by John View Post
    Code:
    str_replace(" """$string); 
    Will bring words together, won't be good for multiword variables.

    Code:
    function clearWhiteSpace($s){
            
    $str trim($s);
            
    $ret_str='';
            for(
    $i=0;$i strlen($str);$i++){
                if(
    substr($str$i1) != " "){
                    
    $ret_str .= trim(substr($str$i1)); 
                }else{ 
                    while(
    substr($str,$i,1) == " "){
                        
    $i++; 
                    } 
                    
    $ret_str.= " "
                    
    $i--; // *** 
                
    }
            } 
            return 
    $ret_str
        } 
    Is good if you want to maintain the word syntax.
    Realize the Web Web services and design.

  6. #5
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: strip white space

    I try to avoid loops if possible. You could simply use the following to preserve single spaces in sentences.

    Code:
    $string preg_replace('/\s{2,}/'' 'trim($string)); 

  7. #6
    mikelbring is offline Programmer
    Join Date
    Jul 2008
    Location
    Nebraska
    Posts
    118
    Rep Power
    0

    Re: strip white space

    ahh thank you. Im not very good with regex.
    Realize the Web Web services and design.

  8. #7
    Jordan Guest

    Re: strip white space

    Be careful with John's regular expression, if all you want to strip out is spaces. The regular expression above strips all whitespace. Example:

    Code:
    <?php
    $string 
    "This is a test\r\nTesting\r\nWhere are mye linebreaks?\r\n";
    echo 
    $string// With line break
    $string preg_replace('/\s{2,}/'' 'trim($string));
    echo 
    $string// Stripped
    ?>
    Output:
    Code:
    This is a test
    Testing
    Where are mye linebreaks?
    This is a test Testing Where are mye linebreaks?
    You could use this just to remove more than one space, preserve Unix style line breaks and keep spacing between words:

    Code:
    $string preg_replace('/ {2,}/'' 'trim($string)); 
    Example:
    Code:
    <?php
    $string 
    "A lot of     spaces   here\r\nPreserves Unix style   line   breaks?\r\n";
    echo 
    $string// With Spaces
    $string preg_replace('/ {2,}/'' 'trim($string));
    echo 
    $string// Stripped and preserves line breaks
    ?>
    Output:
    Code:
    A lot of     spaces   here
    Preserves Unix style   line   breaks?
    A lot of spaces here
    Preserves Unix style line breaks?

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Pokemon Black and White
    By stevie754 in forum Video Game Talk
    Replies: 0
    Last Post: 03-18-2011, 02:22 AM
  2. Replies: 10
    Last Post: 10-27-2009, 05:29 AM
  3. White screen after logged in
    By gamiR in forum Linux Applications
    Replies: 2
    Last Post: 12-08-2008, 06:04 AM
  4. Replies: 7
    Last Post: 11-10-2008, 08:27 AM

Tags for this Thread

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