SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trim more than whitespace

    Hi,

    I have a search functionality on my website, and aswel as using trim to trim white space before and after the keyword, I would also like it to trim anything else out that isnt in the alphabet.

    Here is what I am using:

    PHP Code:
      $var = @$_GET['q'] ;
      
    $trimmed trim($var); 
    Can this be done?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure is, see preg_replace().
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi OK cheers,

    Got the jist of that.

    Do you have anything handy that basically trims everything out other than a letter, and replaces it with nothing

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Code:
    preg_replace('~[^a-z]~i', null, $string);
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Anthony

    Cheers

  6. #6
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /**
         * Strips extra whitespace from output
         *
         * @param string $str String to sanitize
         * @return string whitespace sanitized string
         * @access public
         * @static
         */
        
    public static function stripWhitespace($str) {
            
    $r preg_replace('/[\n\r\t]+/'''$str);
            return 
    preg_replace('/\s{2,}/'' '$r);
        } 
    Above function is extracted from CI Enjoy


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
  •