Simple Regular Expression Removal Class

I have written a simple php class that handles regular expressions. Once instantiated it can remove characters, numbers, words, letters, or custom expressions from a string. It does not require you to know any of the syntax for implementation. It simply returns the value of the string with the pattern removed. This can be helpful as it removes the need for syntax in using preg_replace, or preg_match. It also can take care of cleaning $_GET and $_POST variables before insertion into a database. Here is it being using in its most basic form
        
        // Define the string
        $string = "12345H5 34%#!dr";

        // Require the class
        require "/path/to/EasyRegRexRemoval.php";       
        
        // Instantiate the class
        $regex = new regExRemove();     
        
        // Remove all numbers from the string
        echo $regex->removeNumbers($string);
        
        // Outputs
        H %#!dr
        
        // Remove all lowercase letters from a string
        echo $regex->removeLowercaseLetters($string);
        
        // Outputs
        12345H5 34%#!
        
        // There is a range method too.  Like...remove all number in the range 0-4 from a string
        echo $regex->removeRange("0-4", $string);
        
        // Outputs
        5H5 %#!dr
        
        // Range also works with letters (case sensitive)
        echo $regex->removeRange("a-d", $string);
        
        // Outputs
        12345H5 34%#!r

        // Remove all non-alphanumeric characters
        echo $regex->removeNonAlphaNumeric($string);
        
        // Outputs
        12345H534dr
                
        // Remove all whitespace        
        echo $regex->removeWhiteSpace($string);
        
        // Outputs
        12345H534%#!dr
        
The different methods can also be combined

        // Define the string
        $string = "12345H5 34%#!dr";

        // Require the class
        require "/path/to/EasyRegRexRemoval.php";       
        
        // Instantiate the class
        $regex = new regExRemove();     
        
        // Remove Numbers and Letters
        $string = $regex->removeNumbers($string);
        echo $regex->removeLetters($string);
        
        // Outputs
        %#!
                        
        // I have alse included an "echo" argument that allows you to use inline in HTML or database insertion (not crazy, i know, but thought it was nice : > )
        <span>Here is my string: <?php $regex->removeLetters($string, "echo"); ?></span>
        
There is also custom usage too.

        // Define the string
        $string = "12345H5 34%#!dr";

        // Require the class
        require "/path/to/EasyRegRexRemoval.php";       
        
        // Instantiate the class
        $regex = new regExRemove();
        
        //if you want to find and remove a certain whole word or phrase use as such
        $sentance = "Who farted something fierce?";
        $regex->removeCustom("something", $sentance);
        
        // Outputs
        Who farted fierce?
        
        //if you want to remove all the numbers between 4-10 and also letters between a-f, use as such
        $regex->removeCustom("[4-10a-f]");
        
        // If you do not know the proper syntax for some regular expressions, you can use the following example

        $string = $regex->removeRange("4-10", $string);
        $string = $regex->removeRange("a-f", $string);
        echo $string;
        
        // Outputs 
        123H %#!r

                
All The methods in the class are as follows

        // Require the class
        require "/path/to/EasyRegRexRemoval.php";       
        
        // Instantiate the class
        $regex = new regExRemove();
        
        $regex->removeNumbers();
        $regex->removeRange();
        $regex->removeLowercaseLetters();
        $regex->removeUppercaseLetters();
        $regex->removeLetters();
        $regex->removeNumbersLetters();
        $regex->removeNonAlphaNumeric();
        $regex->removeWhiteSpace();
        $regex->removeCustom();
        
Here is the class itself. Feel free to modify, edit, add to it. Enjoy!

/*
Regular Expression Removal Class
Author: Matthew Price
URL: http://matthewaprice.com
        
        This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
*/

class regExRemove {

        private function regExRemoveWorker($expression,$string,$getBack) {
                        
                                $newString = preg_replace($expression,"",$string);                                              
                
                if ($getBack == "echo") {
                        echo $newString;
                        } else {
                        return $newString;      
                }
        }

        public function removeNumbers($string,$getBack) {
                
                return $this->regExRemoveWorker("/[0-9]/",$string,$getBack);
                                        
        }

        public function removeRange($range,$string,$getBack) {
                
                return $this->regExRemoveWorker("/[" . $range . "]/",$string,$getBack);
                                        
        }
                
        public function removeLowercaseLetters($string,$getBack) {

                return $this->regExRemoveWorker("/[a-z]/",$string,$getBack);

        }
        
        public function removeUppercaseLetters($string,$getBack) {

                return $this->regExRemoveWorker("/[A-Z]/",$string,$getBack);

        }

        public function removeLetters($string,$getBack) {

                return $this->regExRemoveWorker("/[a-zA-Z]/",$string,$getBack);

        }
                
        public function removeNumbersLetters($string,$getBack) {

                return $this->regExRemoveWorker("/[a-zA-Z0-9]/",$string,$getBack);

        }
        
        public function removeNonAlphaNumeric($string,$getBack) {

                return $this->regExRemoveWorker("/[^A-Za-z0-9]/",$string,$getBack);

        }
        
        public function removeWhiteSpace($string,$getBack) {

                return $this->regExRemoveWorker("/[s]/",$string);

        }       

        public function removeCustom($expression,$string,$getBack) {

                return $this->regExRemoveWorker("/" . $expression . "/",$string,$getBack);

        }               
        
}

Post a comment





Real Time Web Analytics ^