[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-matchesmapregex.php (source)

   1  <?php
   2  /**
   3   * WP_MatchesMapRegex helper class
   4   *
   5   * @package WordPress
   6   * @since 4.7.0
   7   */
   8  
   9  /**
  10   * Helper class to remove the need to use eval to replace $matches[] in query strings.
  11   *
  12   * @since 2.9.0
  13   */
  14  class WP_MatchesMapRegex {
  15      /**
  16       * store for matches
  17       *
  18       * @var array
  19       */
  20      private $_matches;
  21  
  22      /**
  23       * store for mapping result
  24       *
  25       * @var string
  26       */
  27      public $output;
  28  
  29      /**
  30       * subject to perform mapping on (query string containing $matches[] references
  31       *
  32       * @var string
  33       */
  34      private $_subject;
  35  
  36      /**
  37       * regexp pattern to match $matches[] references
  38       *
  39       * @var string
  40       */
  41      public $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // Magic number.
  42  
  43      /**
  44       * constructor
  45       *
  46       * @param string $subject subject if regex
  47       * @param array  $matches data to use in map
  48       */
  49  	public function __construct( $subject, $matches ) {
  50          $this->_subject = $subject;
  51          $this->_matches = $matches;
  52          $this->output   = $this->_map();
  53      }
  54  
  55      /**
  56       * Substitute substring matches in subject.
  57       *
  58       * static helper function to ease use
  59       *
  60       * @param string $subject subject
  61       * @param array  $matches data used for substitution
  62       * @return string
  63       */
  64  	public static function apply( $subject, $matches ) {
  65          $oSelf = new WP_MatchesMapRegex( $subject, $matches );
  66          return $oSelf->output;
  67      }
  68  
  69      /**
  70       * do the actual mapping
  71       *
  72       * @return string
  73       */
  74  	private function _map() {
  75          $callback = array( $this, 'callback' );
  76          return preg_replace_callback( $this->_pattern, $callback, $this->_subject );
  77      }
  78  
  79      /**
  80       * preg_replace_callback hook
  81       *
  82       * @param array $matches preg_replace regexp matches
  83       * @return string
  84       */
  85  	public function callback( $matches ) {
  86          $index = (int) substr( $matches[0], 9, -1 );
  87          return ( isset( $this->_matches[ $index ] ) ? urlencode( $this->_matches[ $index ] ) : '' );
  88      }
  89  }


Generated: Sun Aug 1 01:00:05 2021 Cross-referenced by PHPXref 0.7.1