[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Fri Apr 26 08:20:02 2024 Cross-referenced by PHPXref