|
Please consider merging this patch into replace_plus. Hopefully the example I’ve added will make it clear:
diff --git a/third_party/replace_plus/pi.replace_plus.php b/third_party/replace_plus/pi.replace_plus.php --- a/third_party/replace_plus/pi.replace_plus.php +++ b/third_party/replace_plus/pi.replace_plus.php @@ -48,6 +48,7 @@ class Replace_plus { $multiple_param = $this->EE->TMPL->fetch_param('multiple'); $casesensitive_param = $this->EE->TMPL->fetch_param('casesensitive'); $regex_param = $this->EE->TMPL->fetch_param('regex'); + $regex_modifiers = $this->EE->TMPL->fetch_param('regex_modifiers'); $regex_limit = $this->EE->TMPL->fetch_param('regex_limit') ? $this->EE->TMPL->fetch_param('regex_limit') : -1; // clean up tag's params @@ -72,7 +73,7 @@ class Replace_plus { // II. Perform find-replace - $tagdata = $this->perform_find_replace($tagdata, $find_param, $replace_param, $multiple_param, $casesensitive_param, $regex_param, $regex_limit); + $tagdata = $this->perform_find_replace($tagdata, $find_param, $replace_param, $multiple_param, $casesensitive_param, $regex_param, $regex_modifiers, $regex_limit); // output tagdata $this->return_data = $tagdata; @@ -127,12 +128,14 @@ class Replace_plus { //echo 'casesensitive: '.$casesensitive.'<br><br>'; $regex = $this->fetch_area_parameter($parameters, 'regex'); //echo 'regex: '.$regex.'<br><br>'; + $regex_modifiers = $this->fetch_area_parameter($parameters, 'regex_modifiers'); + //echo 'regex_modifiers: '.$regex_modifiers.'<br><br>'; $regex_limit = $this->fetch_area_parameter($parameters, 'regex_limit'); //echo 'regex_limit: '.$regex_limit.'<br><br>'; // V. Perform find-replace - $replace_area = $this->perform_find_replace($replace_area, $find, $replace, $multiple, $casesensitive, $regex, $regex_limit); + $replace_area = $this->perform_find_replace($replace_area, $find, $replace, $multiple, $casesensitive, $regex, $regex_modifiers, $regex_limit); // assemble tagdata $tagdata = $tagdata_first_half.$replace_area.$tagdata_second_half_splitted[1]; @@ -195,6 +198,9 @@ of the param "find" is a regular expression. 6) regex_limit - Optional. The maximum possible of regex replacements. Default value is -1 (no limit). +7) regex_modifiers - Optional. Append regular expression modifiers to the end of the regular expression. +(http://php.net/manual/en/reference.pcre.pattern.modifiers.php). + VARIABLE PAIRS {replace_area}{/replace_area} - Optional. allows you to specify area in which the plugin should do find-replace @@ -323,6 +329,16 @@ Result: * * * * Result: text (http://www.foo.com/) you want processed (http://www.bar.com/) +# Regular Expression to isolate tag using a modifier to ignore newlines. +{exp:replace_plus find=".*<[Hh]2[^>]*>(.*)<\\/[Hh]2>.*" replace="$1" regex="yes" regex_modifiers="s"} + <h1>Text you want Processed</h1> + Lorum Ipsum + <h2 class='myClass'>More text you want processed</h2> + Foo Bar +{/exp:replace_plus} + +Result: More text you want processed + Note: if you want to replace something with nothing, best is to omit the replace parameter altogether. If you want to find multiple strings, always use the multiple="yes" parameter, or else it will search for the literal string, including vertical bars. The multiple parameter has no effect when using a @@ -380,7 +396,7 @@ function fetch_area_parameter($param_string, $param_name) return $param_value; } -function perform_find_replace($data, $find, $replace, $multiple, $casesensitive, $regex, $regex_limit) +function perform_find_replace($data, $find, $replace, $multiple, $casesensitive, $regex, $regex_modifiers, $regex_limit) { // regular expression replace if ($regex == 'yes' AND $find !== FALSE AND ($replace OR $replace === '')) @@ -400,6 +416,13 @@ function perform_find_replace($data, $find, $replace, $multiple, $casesensitive, { $find .= "i"; } + + // add other modifiers + if (! empty($regex_modifiers)) + { + $find .= $regex_modifiers; + } + $data = preg_replace($find, $replace, $data, $regex_limit); } // normal str_replace @@ -547,4 +570,3 @@ if (!function_exists('str_ireplace')) { -?> \ No newline at end of file
|