|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Filter |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: PregReplace.php 21085 2010-02-18 21:08:39Z thomas $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Filter_Interface |
|
24 */ |
|
25 require_once 'Zend/Filter/Interface.php'; |
|
26 |
|
27 /** |
|
28 * @category Zend |
|
29 * @package Zend_Filter |
|
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
32 */ |
|
33 class Zend_Filter_PregReplace implements Zend_Filter_Interface |
|
34 { |
|
35 /** |
|
36 * Pattern to match |
|
37 * @var mixed |
|
38 */ |
|
39 protected $_matchPattern = null; |
|
40 |
|
41 /** |
|
42 * Replacement pattern |
|
43 * @var mixed |
|
44 */ |
|
45 protected $_replacement = ''; |
|
46 |
|
47 /** |
|
48 * Is unicode enabled? |
|
49 * |
|
50 * @var bool |
|
51 */ |
|
52 static protected $_unicodeSupportEnabled = null; |
|
53 |
|
54 /** |
|
55 * Is Unicode Support Enabled Utility function |
|
56 * |
|
57 * @return bool |
|
58 */ |
|
59 static public function isUnicodeSupportEnabled() |
|
60 { |
|
61 if (self::$_unicodeSupportEnabled === null) { |
|
62 self::_determineUnicodeSupport(); |
|
63 } |
|
64 |
|
65 return self::$_unicodeSupportEnabled; |
|
66 } |
|
67 |
|
68 /** |
|
69 * Method to cache the regex needed to determine if unicode support is available |
|
70 * |
|
71 * @return bool |
|
72 */ |
|
73 static protected function _determineUnicodeSupport() |
|
74 { |
|
75 self::$_unicodeSupportEnabled = (@preg_match('/\pL/u', 'a')) ? true : false; |
|
76 } |
|
77 |
|
78 /** |
|
79 * Constructor |
|
80 * Supported options are |
|
81 * 'match' => matching pattern |
|
82 * 'replace' => replace with this |
|
83 * |
|
84 * @param string|array $options |
|
85 * @return void |
|
86 */ |
|
87 public function __construct($options = null) |
|
88 { |
|
89 if ($options instanceof Zend_Config) { |
|
90 $options = $options->toArray(); |
|
91 } else if (!is_array($options)) { |
|
92 $options = func_get_args(); |
|
93 $temp = array(); |
|
94 if (!empty($options)) { |
|
95 $temp['match'] = array_shift($options); |
|
96 } |
|
97 |
|
98 if (!empty($options)) { |
|
99 $temp['replace'] = array_shift($options); |
|
100 } |
|
101 |
|
102 $options = $temp; |
|
103 } |
|
104 |
|
105 if (array_key_exists('match', $options)) { |
|
106 $this->setMatchPattern($options['match']); |
|
107 } |
|
108 |
|
109 if (array_key_exists('replace', $options)) { |
|
110 $this->setReplacement($options['replace']); |
|
111 } |
|
112 } |
|
113 |
|
114 /** |
|
115 * Set the match pattern for the regex being called within filter() |
|
116 * |
|
117 * @param mixed $match - same as the first argument of preg_replace |
|
118 * @return Zend_Filter_PregReplace |
|
119 */ |
|
120 public function setMatchPattern($match) |
|
121 { |
|
122 $this->_matchPattern = $match; |
|
123 return $this; |
|
124 } |
|
125 |
|
126 /** |
|
127 * Get currently set match pattern |
|
128 * |
|
129 * @return string |
|
130 */ |
|
131 public function getMatchPattern() |
|
132 { |
|
133 return $this->_matchPattern; |
|
134 } |
|
135 |
|
136 /** |
|
137 * Set the Replacement pattern/string for the preg_replace called in filter |
|
138 * |
|
139 * @param mixed $replacement - same as the second argument of preg_replace |
|
140 * @return Zend_Filter_PregReplace |
|
141 */ |
|
142 public function setReplacement($replacement) |
|
143 { |
|
144 $this->_replacement = $replacement; |
|
145 return $this; |
|
146 } |
|
147 |
|
148 /** |
|
149 * Get currently set replacement value |
|
150 * |
|
151 * @return string |
|
152 */ |
|
153 public function getReplacement() |
|
154 { |
|
155 return $this->_replacement; |
|
156 } |
|
157 |
|
158 /** |
|
159 * Perform regexp replacement as filter |
|
160 * |
|
161 * @param string $value |
|
162 * @return string |
|
163 */ |
|
164 public function filter($value) |
|
165 { |
|
166 if ($this->_matchPattern == null) { |
|
167 require_once 'Zend/Filter/Exception.php'; |
|
168 throw new Zend_Filter_Exception(get_class($this) . ' does not have a valid MatchPattern set.'); |
|
169 } |
|
170 |
|
171 return preg_replace($this->_matchPattern, $this->_replacement, $value); |
|
172 } |
|
173 |
|
174 } |