WP_MatchesMapRegex

Helper class to remove the need to use eval to replace $matches[] in query strings.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp.php  
  1. class WP_MatchesMapRegex { 
  2. /** 
  3. * store for matches 
  4. * @access private 
  5. * @var array 
  6. */ 
  7. private $_matches; 
  8.  
  9. /** 
  10. * store for mapping result 
  11. * @access public 
  12. * @var string 
  13. */ 
  14. public $output; 
  15.  
  16. /** 
  17. * subject to perform mapping on (query string containing $matches[] references 
  18. * @access private 
  19. * @var string 
  20. */ 
  21. private $_subject; 
  22.  
  23. /** 
  24. * regexp pattern to match $matches[] references 
  25. * @var string 
  26. */ 
  27. public $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number 
  28.  
  29. /** 
  30. * constructor 
  31. * @param string $subject subject if regex 
  32. * @param array $matches data to use in map 
  33. */ 
  34. public function __construct($subject, $matches) { 
  35. $this->_subject = $subject; 
  36. $this->_matches = $matches; 
  37. $this->output = $this->_map(); 
  38.  
  39. /** 
  40. * Substitute substring matches in subject. 
  41. * static helper function to ease use 
  42. * @static 
  43. * @access public 
  44. * @param string $subject subject 
  45. * @param array $matches data used for substitution 
  46. * @return string 
  47. */ 
  48. public static function apply($subject, $matches) { 
  49. $oSelf = new WP_MatchesMapRegex($subject, $matches); 
  50. return $oSelf->output; 
  51.  
  52. /** 
  53. * do the actual mapping 
  54. * @access private 
  55. * @return string 
  56. */ 
  57. private function _map() { 
  58. $callback = array($this, 'callback'); 
  59. return preg_replace_callback($this->_pattern, $callback, $this->_subject); 
  60.  
  61. /** 
  62. * preg_replace_callback hook 
  63. * @access public 
  64. * @param array $matches preg_replace regexp matches 
  65. * @return string 
  66. */ 
  67. public function callback($matches) { 
  68. $index = intval(substr($matches[0], 9, -1)); 
  69. return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );