Plugin

Archived
Forum
(read-only)

Find and Replace Plus

ExpressionEngine 1.x, ExpressionEngine 2

Back to this add-on's main page
View Other Add-ons From Laisvunas

     

PCRE modifiers as a parameter for regex replaces

Feature Request

smbc
smbc

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,+48,@@ 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,+73,@@ 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,+198,@@ of the param "find" is a regular expression.
 
 
6regex_limit OptionalThe maximum possible of regex replacements. Default value is -(no limit).
 
+
7regex_modifiers OptionalAppend 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} Optionalallows you to specify area in which the plugin should do find-replace
@@ -323,+329,16 @@ Result: * * * *
 
 
Resulttext (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}
+
+
ResultMore text you want processed
+
 
Note: if you want to replace something with nothingbest is to omit the replace parameter altogether.
 If 
you want to find multiple stringsalways use the multiple="yes" parameter, or else it will search
 
for the literal stringincluding vertical barsThe multiple parameter has no effect when using a
@@ -380,+396,@@ 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,+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,+570,@@ if (!function_exists('str_ireplace')) {
 
 
     
-?>
\ No newline at end of file