rfc:preg_replace_callback_array
Differences
This shows you the differences between two versions of the page.
rfc:preg_replace_callback_array [2015/03/10 13:54] demon |
rfc:preg_replace_callback_array [2017/09/22 13:28] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Add preg_replace_callback_array function ====== | ||
- | * Version: 1.0 | ||
- | * Date: 2015-03-10 | ||
- | * Author: Wei Dai, demon@php.net | ||
- | * Status: Draft | ||
- | * First Published at: https:// | ||
- | |||
- | ===== Introduction ===== | ||
- | Before 5.5.0, we can use the '/ | ||
- | <code php Zend/ | ||
- | $code = preg_replace( | ||
- | array( | ||
- | "/ | ||
- | "/ | ||
- | "/ | ||
- | "/ | ||
- | ), | ||
- | array( | ||
- | " | ||
- | " | ||
- | "' | ||
- | "' | ||
- | ), | ||
- | $code); | ||
- | </ | ||
- | |||
- | Since 5.5.0, we deprecated the /e modifier, so we have to change it to: | ||
- | <code php Zend/ | ||
- | $code = preg_replace_callback( | ||
- | array( | ||
- | "/ | ||
- | "/ | ||
- | "/ | ||
- | "/ | ||
- | ), | ||
- | function($matches) use ($spec, $prefix, $op1, $op2) { | ||
- | if (strncasecmp($matches[0], | ||
- | return " | ||
- | } else if (strncasecmp($matches[0], | ||
- | return " | ||
- | } else if (strncasecmp($matches[0], | ||
- | return " | ||
- | } else { | ||
- | return " | ||
- | } | ||
- | }, $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, | ||
- | |||
- | With the preg_replace_callback_array function, the code is: | ||
- | <code php Zend/ | ||
- | $code = preg_replace_callback_array( | ||
- | array( | ||
- | "/ | ||
- | return " | ||
- | }, | ||
- | "/ | ||
- | | ||
- | }, | ||
- | "/ | ||
- | return " | ||
- | }, | ||
- | "/ | ||
- | return " | ||
- | }, | ||
- | ), $code); | ||
- | </ | ||
- | |||
- | 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) ===== | ||
- | This is proposed for PHP7 | ||
- | |||
- | ===== Unaffected PHP Functionality ===== | ||
- | preg_filter(), | ||
- | |||
- | ===== Proposed Voting Choices ===== | ||
- | Include these so readers know where you are heading and can discuss the proposed voting options. | ||
- | |||
- | State whether this project requires a 2/3 or 50%+1 majority (see [[voting]]) | ||
- | |||
- | ===== Patches and Tests ===== | ||
- | Write it later. | ||
rfc/preg_replace_callback_array.txt · Last modified: 2017/09/22 13:28 (external edit)