From 4c7231d2b83f93f433469cef4a75339cb0d90d00 Mon Sep 17 00:00:00 2001 From: William Cahill-Manley Date: Wed, 11 Jan 2012 15:42:25 -0800 Subject: [PATCH] Allow capture group substitution in case content This adds numeric and named capture group replacement to case content return data. Example: {exp:switchee variable="{segment_3}"} {!-- detect pagination, for example --} {case value="#^P(?P\d+)$#|''"} We are on page #{switchee:page} {/case} {/exp:switchee} --- pi.switchee.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pi.switchee.php b/pi.switchee.php index a97189f..852efbe 100644 --- a/pi.switchee.php +++ b/pi.switchee.php @@ -154,7 +154,7 @@ public function Switchee() // check for a string contained within hashes #regex# if (preg_match('/^#(.*)#$/', $case_value)) { - if (preg_match($case_value, $var)) + if (preg_match($case_value, $var, $case_vars)) { // we've found a match, grab case content and exit loop $this->return_data = substr($tagdata, $starts_at, $ends_at - $starts_at); @@ -164,6 +164,19 @@ public function Switchee() { $this->EE->TMPL->log_item("Switchee: regex match: case '{$case_value}' matched variable '{$var}'"); } + + if ( count($case_vars) > 1) + { // pattern used capture groups, make them available as variables + foreach( $case_vars as $case_var_key => $case_var_value) + { + if ($debug) + { + $this->EE->TMPL->log_item("Switchee: captured variable: '{switchee:{$case_var_key}}' => '{$case_var_value}'"); + } + + $this->return_data = preg_replace("#{switchee:{$case_var_key}}#", $case_var_value, $this->return_data); + } + } break 2; }