Is the regular expression-ThinkPHP incorrect? /{| (W + )}/

Source: Internet
Author: User
Using ThinkPHP3.2.2, I read a line of code like this in a file: {code...} 1) Is this section {| (w +)} wrong? Curly braces {}, separator | isn't it regular? But it runs normally. $ Rule value example: {code...} 2) he intended to read a line of code like this in a file using ThinkPHP3.2.2:

# File ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior. class. php # method # {| FUN} use functions $ rule = preg_replace_callback ('/{| (\ w +)}/', function ($ match) {return $ match [1] () ;}, $ rule );

1) Is this/{| (\ w +)}/incorrect?
Curly braces {}, separator | isn't it regular? But it runs normally.

$ Rule value example:

$rule = '{:module}/{:controller}/{:action}/{$_SERVER.REQUEST_URI|md5}';

2) The intention is to perform MD5 for $ _ SERVER. REQUEST_URI and then return it.
But in that piece of code, does it pass in the value?

return $match[1]();

Complete File Code

// Determine whether static cache is required. static private function requireHtmlCache () {// analyze the current static rule $ htmls = C ('html _ CACHE_RULES '); // read the static Rule if (! Empty ($ htmls) {$ htmls = array_change_key_case ($ htmls); // static rule file Definition Format actionName => array ('static rule', 'cache time ', 'additional rule') // 'read' => array ('{id}, {name}', 60, 'md5 ') the uniqueness and enforceability of the static rule must be ensured. // The static rule $ controllerName = strtolower (CONTROLLER_NAME); $ actionName = strtolower (ACTION_NAME); if (isset ($ htmls [$ controllerName. ':'. $ actionName]) {$ html = $ htmls [$ controllerName. ':'. $ actionName]; // static rule for operations on a controller} elseif (isset ($ Htmls [$ controllerName. ':']) {// static rule of a controller $ html = $ htmls [$ controllerName. ':'];} elseif (isset ($ htmls [$ actionName]) {$ html = $ htmls [$ actionName]; // static rules for all operations} elseif (isset ($ htmls ['*']) {$ html = $ htmls ['*']; // Global static rule} if (! Empty ($ html) {// interpret static rules $ rule = is_array ($ html )? $ Html [0]: $ html; // system variable starting with $ _ $ callback = function ($ match) {switch ($ match [1]) {case '_ get': $ var =$ _ GET [$ match [2]; break; case' _ Post ': $ var = $ _ POST [$ match [2]; break; case '_ request': $ var = $ _ REQUEST [$ match [2]; break; case '_ Server': $ var = $ _ SERVER [$ match [2]; break; case' _ SESSION ': $ var = $ _ SESSION [$ match [2]; break; case '_ cookies': $ var = $ _ COOKIE [$ match [2]; break ;} return (count ($ match) = 4 )? $ Match [3] ($ var): $ var ;}; $ rule = preg_replace_callback ('/{\$ (_ \ w +) \. (\ w + )(? :\| (\ W + ))?} /', $ Callback, $ rule); // {ID | FUN} $ rule = preg_replace_callback ('/{(\ w +) \ | (\ w +)}/', function ($ match) {return $ match [2] ($ _ GET [$ match [1]);}, $ rule); $ rule = preg_replace_callback ('/{(\ w +)}/', function ($ match) {return $ _ GET [$ match [1] ;}, $ rule); // special system variable $ rule = str_ireplace (array ('{: controller }', '{: action}', '{: module}'), array (CONTROLLER_NAME, ACTION_NAME, MODULE_NAME), $ rule); // {| FUN} Use the functions $ rule = preg_replace_callback ('/{| (\ w +)}/', function ($ match) {return $ match [1] ();}, $ rule); $ cacheTime = C ('html _ CACHE_TIME ', null, 60); if (is_array ($ HTML) {if (! Empty ($ html [2]) $ rule = $ html [2] ($ rule); // apply the additional function $ cacheTime = isset ($ html [1])? $ Html [1]: $ cacheTime; // cache validity period} else {$ cacheTime = $ cacheTime;} // The current cache file define ('html _ FILE_NAME ', HTML_PATH. export rule.c('html_file_suffix', null,'.html '); return $ cacheTime ;}// return false without caching ;}

Reply content:

Using ThinkPHP3.2.2, read the following code in a file:

# File ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior. class. php # method # {| FUN} use functions $ rule = preg_replace_callback ('/{| (\ w +)}/', function ($ match) {return $ match [1] () ;}, $ rule );

1) Is this/{| (\ w +)}/incorrect?
Curly braces {}, separator | isn't it regular? But it runs normally.

$ Rule value example:

$rule = '{:module}/{:controller}/{:action}/{$_SERVER.REQUEST_URI|md5}';

2) The intention is to perform MD5 for $ _ SERVER. REQUEST_URI and then return it.
But in that piece of code, does it pass in the value?

return $match[1]();

Complete File Code

// Determine whether static cache is required. static private function requireHtmlCache () {// analyze the current static rule $ htmls = C ('html _ CACHE_RULES '); // read the static Rule if (! Empty ($ htmls) {$ htmls = array_change_key_case ($ htmls); // static rule file Definition Format actionName => array ('static rule', 'cache time ', 'additional rule') // 'read' => array ('{id}, {name}', 60, 'md5 ') the uniqueness and enforceability of the static rule must be ensured. // The static rule $ controllerName = strtolower (CONTROLLER_NAME); $ actionName = strtolower (ACTION_NAME); if (isset ($ htmls [$ controllerName. ':'. $ actionName]) {$ html = $ htmls [$ controllerName. ':'. $ actionName]; // static rule for operations on a controller} elseif (isset ($ Htmls [$ controllerName. ':']) {// static rule of a controller $ html = $ htmls [$ controllerName. ':'];} elseif (isset ($ htmls [$ actionName]) {$ html = $ htmls [$ actionName]; // static rules for all operations} elseif (isset ($ htmls ['*']) {$ html = $ htmls ['*']; // Global static rule} if (! Empty ($ html) {// interpret static rules $ rule = is_array ($ html )? $ Html [0]: $ html; // system variable starting with $ _ $ callback = function ($ match) {switch ($ match [1]) {case '_ get': $ var =$ _ GET [$ match [2]; break; case' _ Post ': $ var = $ _ POST [$ match [2]; break; case '_ request': $ var = $ _ REQUEST [$ match [2]; break; case '_ Server': $ var = $ _ SERVER [$ match [2]; break; case' _ SESSION ': $ var = $ _ SESSION [$ match [2]; break; case '_ cookies': $ var = $ _ COOKIE [$ match [2]; break ;} return (count ($ match) = 4 )? $ Match [3] ($ var): $ var ;}; $ rule = preg_replace_callback ('/{\$ (_ \ w +) \. (\ w + )(? :\| (\ W + ))?} /', $ Callback, $ rule); // {ID | FUN} $ rule = preg_replace_callback ('/{(\ w +) \ | (\ w +)}/', function ($ match) {return $ match [2] ($ _ GET [$ match [1]);}, $ rule); $ rule = preg_replace_callback ('/{(\ w +)}/', function ($ match) {return $ _ GET [$ match [1] ;}, $ rule); // special system variable $ rule = str_ireplace (array ('{: controller }', '{: action}', '{: module}'), array (CONTROLLER_NAME, ACTION_NAME, MODULE_NAME), $ rule); // {| FUN} Use the functions $ rule = preg_replace_callback ('/{| (\ w +)}/', function ($ match) {return $ match [1] ();}, $ rule); $ cacheTime = C ('html _ CACHE_TIME ', null, 60); if (is_array ($ HTML) {if (! Empty ($ html [2]) $ rule = $ html [2] ($ rule); // apply the additional function $ cacheTime = isset ($ html [1])? $ Html [1]: $ cacheTime; // cache validity period} else {$ cacheTime = $ cacheTime;} // The current cache file define ('html _ FILE_NAME ', HTML_PATH. export rule.c('html_file_suffix', null,'.html '); return $ cacheTime ;}// return false without caching ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.