Archive

Posts Tagged ‘Search for patterns’

Search for patterns in text using regular expressions

September 19, 2011 Leave a comment

Few times i was in need to find some pieces of code to do some stuff. Searching for some pattern in text can be easy, and this class i made makes it look neat. In this article some terms will be used, like regular expressions. This article is not going to show how to work with regular expressions ( in future text: regEx ).

I will give you few useful resources to find out more if you are not familiar with regEx:

Class construction

This class is going to be pretty simple. It is going to have only six methods including constructor in which will some initialization happen. This class is going to have two setter methods, one for set text in which will search stuff happen, and one for pattern setting. One of methods will be search method which does not take any parameters. This class is basically heart of this class. It initializes search inside haystack text. There are also going to be two methods to return data. One to return array of results, and one to return string representation of search result. Class also contains three protected properties which are part of mechanism. I am not going to dissect every method separately because they are well explained with in-code comments and provided class documentation. Here is class source code

/**
 * This class is used to do a search over needle in a haystack
 * @author Bojan Bijelic <bojan.bijelic.os@gmial.com>
 */
class SearchPattern {

     /**
      * This is haystack, subject of search
      * @access protected
      * @var string
      */
     protected $_haystack;

     /**
      * This is needle, pattern of search
      * @access protected
      * @var string
      */
     protected $_needle;

     /**
      * This is array of all matches of search
      * @access protected
      * @var array
      */
     protected $_matches;

     /**
      * This is constructor method
      */
     public function __construct(){
          // Inizialize _matches as array type
          $this -> _matches = array();
     }

     /**
      * This is setter of haystack property
      * @param string $haystack
      * @access public
      */
     public function setHaystack( $haystack ){
          // Check if parameter is string and not empty
          if( is_string( $haystack ) && !empty( $haystack ) ){
               // Set haystack property
               $this -> _haystack = $haystack;
          }
     }

     /**
      * This is setter of needle property
      * @param string $needle
      * @access public
      */
     public function setNeedle( $needle ){
          // Check if parameter is string and not empty
          if( is_string( $needle ) && !empty( $needle ) ){
               // Set haystack property
               $this -> _needle = $needle;
          }
     }

     /**
      * This method is used to do search over needle in a haystack
      * @access public
      */
     public function search(){
          // Search in haystack for needles
          // Output matches into array
          preg_match_all( $this -> _needle, $this -> _haystack, $this -> _matches );
     }

     /**
      * This method returns result array
      * @access public
      * @return array
      */
     public function toArray(){
          // Return array
          return $this -> _matches;
     }

     /**
      * This method returns result string
      * Every result item is formated to a new row
      * @access public
      * @return string
      */
     public function toString(){
          // Initialize output string variable
          $output = "";
          // Foreach match
          foreach( $this -> _matches[0] as $match ) $output .= $match . "<br/>";
          // Finally, return stringž
          return $output;
     }
}
Within download package is provided very simple example code for you to see what can be achieved with this small, but powerful class.

Download source code, documentation and example package

Search Pattern.rar

 

For projects mail me on :
bhavinrana07[@]gmail.com