home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / util / regexp / Regexp.php < prev    next >
Encoding:
PHP Script  |  2006-09-14  |  4.9 KB  |  168 lines

  1. <?php
  2. /* 
  3.  *  $Id: Regexp.php 123 2006-09-14 20:19:08Z mrook $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://phing.info>. 
  20.  */
  21.  
  22. /**
  23.  * A factory class for regex functions.
  24.  * @author Hans Lellelid <hans@xmpl.org>
  25.  * @package  phing.util.regexp
  26.  * @version $Revision: 1.5 $
  27.  */
  28. class Regexp {
  29.  
  30.     /**
  31.      * Matching groups found. 
  32.      * @var array
  33.      */
  34.     private $groups = array();
  35.      
  36.     /**
  37.      * Pattern to match.
  38.      * @var string
  39.      */
  40.     private $pattern;
  41.     
  42.     /**
  43.      * Replacement pattern.
  44.      * @var string
  45.      */
  46.     private $replace;
  47.     
  48.     /**
  49.      * The regex engine -- e.g. 'preg' or 'ereg';
  50.      * @var RegexpEngine
  51.      */
  52.     private $engine;
  53.     
  54.     /**
  55.      * Constructor sets the regex engine to use (preg by default).
  56.      * @param string $_engineType The regex engine to use.
  57.      */
  58.     function __construct($engineType='preg') {        
  59.         if ($engineType == 'preg') {
  60.             include_once 'phing/util/regexp/PregEngine.php';
  61.             $this->engine = new PregEngine();
  62.         } elseif ($engineType == 'ereg') {
  63.             include_once 'phing/util/regexp/EregEngine.php';
  64.             $this->engine = new EregEngine();
  65.         } else {
  66.             throw new BuildException("Invalid engine type for Regexp: " . $engineType);
  67.         }                
  68.     }
  69.  
  70.     /**
  71.      * Sets pattern to use for matching.
  72.      * @param string $pat The pattern to match on.
  73.      * @return void
  74.      */
  75.     public function setPattern($pat) {
  76.         $this->pattern = (string) $pat;        
  77.     }
  78.     
  79.     
  80.     /**
  81.      * Gets pattern to use for matching.
  82.      * @return string The pattern to match on.
  83.      */
  84.     public function getPattern() {
  85.         return $this->pattern;
  86.     }
  87.     
  88.     /**
  89.      * Sets replacement string.
  90.      * @param string $rep The pattern to replace matches with.
  91.      * @return void
  92.      */
  93.     public function setReplace($rep) {
  94.         $this->replace = (string) $rep;
  95.     }
  96.     
  97.     /**
  98.      * Gets replacement string.
  99.      * @return string The pattern to replace matches with.
  100.      * @return void
  101.      */
  102.     public function getReplace() {
  103.         return $this->replace;
  104.     }
  105.     
  106.     /**
  107.      * Performs match of specified pattern against $subject.
  108.      * @param string $subject The subject, on which to perform matches.
  109.      * @return boolean Whether or not pattern matches subject string passed.
  110.      */
  111.     public function matches($subject) {
  112.         if($this->pattern === null) {            
  113.             throw new Exception("No pattern specified for regexp match().");
  114.         }
  115.         return $this->engine->match($this->pattern, $subject, $this->groups);
  116.     }
  117.     
  118.     /**
  119.      * Performs replacement of specified pattern and replacement strings.
  120.      * @param string $subject Text on which to perform replacement.
  121.      * @return string subject after replacement has been performed.
  122.      */
  123.     public function replace($subject) {
  124.         if ($this->pattern === null || $this->replace === null) {
  125.             throw new Exception("Missing pattern or replacement string regexp replace().");
  126.         }        
  127.         return $this->engine->replace($this->pattern, $this->replace, $subject);
  128.     }
  129.     
  130.     /**
  131.      * Get array of matched groups.
  132.      * @return array Matched groups
  133.      */ 
  134.     function getGroups() {
  135.         return $this->groups;
  136.     }
  137.  
  138.     /**
  139.      * Get specific matched group. 
  140.      * @param integer $idx
  141.      * @return string specified group or NULL if group is not set.
  142.      */ 
  143.     function getGroup($idx) { 
  144.         if (!isset($this->groups[$idx])) {
  145.             return null;
  146.         }
  147.         return $this->groups[$idx];
  148.     }
  149.     
  150.     /**
  151.      * Sets whether the regexp matching is case insensitive.
  152.      * (default is false -- i.e. case sensisitive)
  153.      * @param boolean $bit
  154.      */ 
  155.     function setIgnoreCase($bit) {
  156.         $this->engine->setIgnoreCase($bit);
  157.     }
  158.     
  159.     /**
  160.      * Gets whether the regexp matching is case insensitive.
  161.      * @return boolean
  162.      */
  163.     function getIgnoreCase() {
  164.         return $this->engine->getIgnoreCase();
  165.     }
  166.  
  167. ?>