| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: PregEngine.php 325 2007-12-20 15:44:58Z hans $
|
|
|
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 |
require_once 'phing/util/regexp/RegexpEngine.php';
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* PREG Regexp Engine.
|
|
|
26 |
* Implements a regexp engine using PHP's preg_match(), preg_match_all(), and preg_replace() functions.
|
|
|
27 |
*
|
|
|
28 |
* @author hans lellelid, hans@velum.net
|
|
|
29 |
* @package phing.util.regex
|
|
|
30 |
*/
|
|
|
31 |
class PregEngine implements RegexpEngine {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* @var boolean
|
|
|
35 |
*/
|
|
|
36 |
private $ignoreCase = false;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Sets whether or not regex operation is case sensitive.
|
|
|
40 |
* @param boolean $bit
|
|
|
41 |
* @return void
|
|
|
42 |
*/
|
|
|
43 |
function setIgnoreCase($bit) {
|
|
|
44 |
$this->ignoreCase = (boolean) $bit;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Gets whether or not regex operation is case sensitive.
|
|
|
49 |
* @return boolean
|
|
|
50 |
*/
|
|
|
51 |
function getIgnoreCase() {
|
|
|
52 |
return $this->ignoreCase;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* The pattern needs to be converted into PREG style -- which includes adding expression delims & any flags, etc.
|
|
|
57 |
* @param string $pattern
|
|
|
58 |
* @return string prepared pattern.
|
|
|
59 |
*/
|
|
|
60 |
private function preparePattern($pattern)
|
|
|
61 |
{
|
|
|
62 |
return '/'.$pattern.'/'.($this->ignoreCase ? 'i' : '');
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Matches pattern against source string and sets the matches array.
|
|
|
67 |
* @param string $pattern The regex pattern to match.
|
|
|
68 |
* @param string $source The source string.
|
|
|
69 |
* @param array $matches The array in which to store matches.
|
|
|
70 |
* @return boolean Success of matching operation.
|
|
|
71 |
*/
|
|
|
72 |
function match($pattern, $source, &$matches) {
|
|
|
73 |
return preg_match($this->preparePattern($pattern), $source, $matches);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Matches all patterns in source string and sets the matches array.
|
|
|
78 |
* @param string $pattern The regex pattern to match.
|
|
|
79 |
* @param string $source The source string.
|
|
|
80 |
* @param array $matches The array in which to store matches.
|
|
|
81 |
* @return boolean Success of matching operation.
|
|
|
82 |
*/
|
|
|
83 |
function matchAll($pattern, $source, &$matches) {
|
|
|
84 |
return preg_match_all($this->preparePattern($pattern), $source, $matches);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Replaces $pattern with $replace in $source string.
|
|
|
89 |
* References to \1 group matches will be replaced with more preg-friendly
|
|
|
90 |
* $1.
|
|
|
91 |
* @param string $pattern The regex pattern to match.
|
|
|
92 |
* @param string $replace The string with which to replace matches.
|
|
|
93 |
* @param string $source The source string.
|
|
|
94 |
* @return string The replaced source string.
|
|
|
95 |
*/
|
|
|
96 |
function replace($pattern, $replace, $source) {
|
|
|
97 |
// convert \1 -> $1, because we want to use the more generic \1 in the XML
|
|
|
98 |
// but PREG prefers $1 syntax.
|
|
|
99 |
$replace = preg_replace('/\\\(\d+)/', '\$$1', $replace);
|
|
|
100 |
return preg_replace($this->preparePattern($pattern), $replace, $source);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
}
|
|
|
104 |
|