rfc:preg_replace_callback_array

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:preg_replace_callback_array [2015/03/10 13:25]
demon created
rfc:preg_replace_callback_array [2015/05/09 20:44]
nikic implemented
Line 1: Line 1:
-====== PHP RFC: Your Title Here ======+====== PHP RFC: Add preg_replace_callback_array function ======
   * Version: 1.0   * Version: 1.0
   * Date: 2015-03-10   * Date: 2015-03-10
   * Author: Wei Dai, demon@php.net   * Author: Wei Dai, demon@php.net
-  * Status: Draft+  * Status: Implemented (in PHP 7.0)
   * First Published at: https://wiki.php.net/rfc/preg_replace_callback_array   * First Published at: https://wiki.php.net/rfc/preg_replace_callback_array
  
 ===== Introduction ===== ===== Introduction =====
-The preg_replace_callback_array function is an extension to preg_replace_callbackWith the functioneach pattern can easily have specific callback+Before 5.5.0we can use the '/e' modifier. Here is piece of code with 5.4.x:  
- +<code php Zend/zend_vm_gen.php> 
-This is the best way to implement when there are multiple patterns.+$code = preg_replace( 
 +                array( 
 +                    "/EXECUTE_DATA/m", 
 +                    "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m", 
 +                    "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*\)/me", 
 +                    "/ZEND_VM_DISPATCH_TO_HELPER_EX\(\s*([A-Za-z_]*)\s*,\s*[A-Za-z_]*\s*,\s*(.*)\s*\);/me", 
 +                ), 
 +                array( 
 +                    "execute_data", 
 +                    "return \\1".($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)", 
 +                    "'return '.helper_name('\\1',$spec,'$op1','$op2').'(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)'", 
 +                    "'return '.helper_name('\\1',$spec,'$op1','$op2').'(\\2, ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);'", 
 +                ),    
 +                $code); 
 +</code>
  
-===== Proposal ===== +Since 5.5.0, we deprecated the /e modifierso we have to change it to: 
-Below is the previous codewhich is from Zend/zend_vm_gen.php. +<code php Zend/zend_vm_gen.php>
- +
-<code php>+
 $code = preg_replace_callback( $code = preg_replace_callback(
     array(     array(
Line 34: Line 46:
     }, $code);     }, $code);
 </code> </code>
 +
 +===== Proposal =====
 +The preg_replace_callback_array function is an extension to preg_replace_callback. With the function, each pattern can easily have a specific callback.
 +
 +This is the best way to implement when there are multiple patterns.
  
 With preg_replace_callback_array, the code is more readable, and full of logic. There is less crazy branch determination. With preg_replace_callback_array, the code is more readable, and full of logic. There is less crazy branch determination.
  
-<code php>+With the preg_replace_callback_array function, the code is: 
 +<code php Zend/zend_vm_gen.php>
 $code = preg_replace_callback_array( $code = preg_replace_callback_array(
  array(  array(
Line 55: Line 73:
 </code> </code>
  
-The first parameter is an array in this function. In this parameter, Key is parameter, and value is callback. Subject will iterate and match each key. If it is matched, the callback will be called. In the meanwhile, the result will be the new subject and passed to the next match. +The first parameter is an array in this function. In this parameter, Key is pattern, and value is callback. Subject will iterate and match each key. If it is matched, the callback will be called. In the meanwhile, the result will be the new subject and passed to the next match. 
  
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
->= PHP 7.0.0+This is proposed for PHP7
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
Line 70: Line 88:
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-Write it later.+Currently implemented on https://github.com/php/php-src/pull/1171
  
rfc/preg_replace_callback_array.txt · Last modified: 2017/09/22 13:28 (external edit)