wp/wp-includes/class-wp-matchesmapregex.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    44 	 * constructor
    44 	 * constructor
    45 	 *
    45 	 *
    46 	 * @param string $subject subject if regex
    46 	 * @param string $subject subject if regex
    47 	 * @param array  $matches data to use in map
    47 	 * @param array  $matches data to use in map
    48 	 */
    48 	 */
    49 	public function __construct($subject, $matches) {
    49 	public function __construct( $subject, $matches ) {
    50 		$this->_subject = $subject;
    50 		$this->_subject = $subject;
    51 		$this->_matches = $matches;
    51 		$this->_matches = $matches;
    52 		$this->output = $this->_map();
    52 		$this->output   = $this->_map();
    53 	}
    53 	}
    54 
    54 
    55 	/**
    55 	/**
    56 	 * Substitute substring matches in subject.
    56 	 * Substitute substring matches in subject.
    57 	 *
    57 	 *
    58 	 * static helper function to ease use
    58 	 * static helper function to ease use
    59 	 *
    59 	 *
    60 	 * @static
       
    61 	 *
       
    62 	 * @param string $subject subject
    60 	 * @param string $subject subject
    63 	 * @param array  $matches data used for substitution
    61 	 * @param array  $matches data used for substitution
    64 	 * @return string
    62 	 * @return string
    65 	 */
    63 	 */
    66 	public static function apply($subject, $matches) {
    64 	public static function apply( $subject, $matches ) {
    67 		$oSelf = new WP_MatchesMapRegex($subject, $matches);
    65 		$oSelf = new WP_MatchesMapRegex( $subject, $matches );
    68 		return $oSelf->output;
    66 		return $oSelf->output;
    69 	}
    67 	}
    70 
    68 
    71 	/**
    69 	/**
    72 	 * do the actual mapping
    70 	 * do the actual mapping
    73 	 *
    71 	 *
    74 	 * @return string
    72 	 * @return string
    75 	 */
    73 	 */
    76 	private function _map() {
    74 	private function _map() {
    77 		$callback = array($this, 'callback');
    75 		$callback = array( $this, 'callback' );
    78 		return preg_replace_callback($this->_pattern, $callback, $this->_subject);
    76 		return preg_replace_callback( $this->_pattern, $callback, $this->_subject );
    79 	}
    77 	}
    80 
    78 
    81 	/**
    79 	/**
    82 	 * preg_replace_callback hook
    80 	 * preg_replace_callback hook
    83 	 *
    81 	 *
    84 	 * @param  array $matches preg_replace regexp matches
    82 	 * @param  array $matches preg_replace regexp matches
    85 	 * @return string
    83 	 * @return string
    86 	 */
    84 	 */
    87 	public function callback($matches) {
    85 	public function callback( $matches ) {
    88 		$index = intval(substr($matches[0], 9, -1));
    86 		$index = intval( substr( $matches[0], 9, -1 ) );
    89 		return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );
    87 		return ( isset( $this->_matches[ $index ] ) ? urlencode( $this->_matches[ $index ] ) : '' );
    90 	}
    88 	}
    91 }
    89 }