3 * WP_MatchesMapRegex helper class
10 * Helper class to remove the need to use eval to replace $matches[] in query strings.
14 class WP_MatchesMapRegex {
24 * store for mapping result
32 * subject to perform mapping on (query string containing $matches[] references
40 * regexp pattern to match $matches[] references
44 public $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number
49 * @param string $subject subject if regex
50 * @param array $matches data to use in map
52 public function __construct($subject, $matches) {
53 $this->_subject = $subject;
54 $this->_matches = $matches;
55 $this->output = $this->_map();
59 * Substitute substring matches in subject.
61 * static helper function to ease use
66 * @param string $subject subject
67 * @param array $matches data used for substitution
70 public static function apply($subject, $matches) {
71 $oSelf = new WP_MatchesMapRegex($subject, $matches);
72 return $oSelf->output;
76 * do the actual mapping
81 private function _map() {
82 $callback = array($this, 'callback');
83 return preg_replace_callback($this->_pattern, $callback, $this->_subject);
87 * preg_replace_callback hook
90 * @param array $matches preg_replace regexp matches
93 public function callback($matches) {
94 $index = intval(substr($matches[0], 9, -1));
95 return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );