+ Reply to Thread
Results 1 to 7 of 7

Thread: strip white space

  1. #1
    Programming God zeroradius will become famous soon enough zeroradius's Avatar
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    834

    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. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    Re: strip white space

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

  3. #3
    Programming God zeroradius will become famous soon enough zeroradius's Avatar
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    834

    Re: strip white space

    thanks


  4. #4
    Programmer mikelbring is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    Nebraska
    Posts
    115

    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.

  5. #5
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    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)); 

  6. #6
    Programmer mikelbring is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    Nebraska
    Posts
    115

    Re: strip white space

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

  7. #7
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    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?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Replies: 6
    Last Post: 06-09-2009, 12:32 AM
  2. Replies: 7
    Last Post: 11-10-2008, 10:27 AM
  3. Rack.111 - 25G capacity to support the free PHP FTP space
    By wqivblpk in forum Hosting Offers
    Replies: 0
    Last Post: 08-23-2008, 08:46 PM
  4. Replies: 3
    Last Post: 07-09-2008, 06:06 AM
  5. Heap Space and Executable JAR Files
    By Ambuoroko in forum Java Help
    Replies: 1
    Last Post: 03-12-2008, 10:56 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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