Explorar el Código

Proper Occurrence Support for Replace Texts https://opengamepanel.org/forum/viewthread.php?thread_id=5810

own3mall hace 8 años
padre
commit
42dcb78d22

+ 10 - 0
includes/functions.php

@@ -768,4 +768,14 @@ function endsWith($haystack, $needle) {
 	// search forward starting from end minus needle length characters
 	return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
 }
+
+// Super ingenious function from https://stackoverflow.com/questions/5519630/php-preg-replace-x-occurence#answer-17047405
+function preg_replace_nth($pattern, $replacement, $subject, $nth=1) {
+    return preg_replace_callback($pattern,
+        function($found) use (&$pattern, &$replacement, &$nth) {
+                $nth--;
+                if ($nth==0) return preg_replace($pattern, $replacement, reset($found) );
+                return reset($found);
+        }, $subject,$nth);
+}
 ?>

+ 2 - 1
modules/config_games/schema_server_config.xml

@@ -115,6 +115,7 @@
       <xs:element name="var" type="xs:string" />
       <xs:element name="filepath" type="xs:string" />
       <xs:element name="options" type="xs:string" />
+      <xs:element name="occurrence" type="xs:string" minOccurs="0" />
     </xs:choice>
     <xs:attribute name="key" type="xs:string" />
   </xs:complexType>
@@ -300,4 +301,4 @@
       <xs:element name="configuration_files" type="configuration_files_type" minOccurs="0" />
     </xs:sequence>
   </xs:complexType>
-</xs:schema>
+</xs:schema>

+ 62 - 17
modules/gamemanager/cfg_text_replace.php

@@ -133,6 +133,7 @@ foreach($file_replacements as $filepath => $replacements)
 		$default = $replacement['default'];
 		$var = $replacement['var'];
 		$options = $replacement['options'];
+		$occurrence = !isset($replacement['occurrence']) || empty($replacement['occurrence']) || !is_numeric($replacement['occurrence']) || $replacement['occurrence'] < 1 ? false : $replacement['occurrence'];
 		
 		if( !in_array( $options, array("tags","tagValueByName","sc","sqc") ) )
 		{
@@ -174,27 +175,71 @@ foreach($file_replacements as $filepath => $replacements)
 		}
 		else
 		{
-			if ($options == "tags")
-				$file_content = preg_replace("/(<$default$var>)(.*)(<\/$default>)/m", '${1}'.$info_param.'${3}', $file_content, 1);
-			elseif ($options == "tagValueByName")
-				$file_content = preg_replace('/('.$default.'.*name="'.$var.'".*value=)(".*")/m', '${1}"' . str_replace('"', '\"', $info_param) . '"', $file_content, 1);
-			elseif($options == "s")//separated
-				$file_content = preg_replace("/$default/m", "$var $info_param", $file_content, 1);
-			elseif ($options == "q")//quoted
-				$file_content = preg_replace("/$default/m", "$var\"" . str_replace('"', '\"', $info_param) . "\"", $file_content, 1);
-			elseif ($options == "sq")//separated & quoted
-				$file_content = preg_replace("/$default/m", "$var \"" . str_replace('"','\"',$info_param) . "\"", $file_content, 1);
-			elseif ($options == "sc")//separated & ending with a comma (used in JC2MP Example)
-				$file_content = preg_replace("/$default/m", "$var $info_param,", $file_content, 1);
-			elseif ($options == "sqc")//separated & quoted & ending with a comma
-				$file_content = preg_replace("/$default/m", "$var \"" . str_replace('"', '\"', $info_param) . "\",", $file_content, 1);
+			if ($options == "tags"){
+				if($occurrence !== false){
+					$file_content = preg_replace_nth("/(<$default$var>)(.*)(<\/$default>)/m", '${1}'.$info_param.'${3}', $file_content, $occurrence);
+				}else{
+					$file_content = preg_replace("/(<$default$var>)(.*)(<\/$default>)/m", '${1}'.$info_param.'${3}', $file_content, 1);
+				}
+			}
+			elseif ($options == "tagValueByName"){
+				if($occurrence !== false){
+					$file_content = preg_replace_nth('/('.$default.'.*name="'.$var.'".*value=)(".*")/m', '${1}"' . str_replace('"', '\"', $info_param) . '"', $file_content, $occurrence);
+				}else{				
+					$file_content = preg_replace('/('.$default.'.*name="'.$var.'".*value=)(".*")/m', '${1}"' . str_replace('"', '\"', $info_param) . '"', $file_content, 1);
+				}
+			}
+			elseif($options == "s"){//separated
+				if($occurrence !== false){
+					$file_content = preg_replace_nth("/$default/m", "$var $info_param", $file_content, $occurrence);
+				}else{
+					$file_content = preg_replace("/$default/m", "$var $info_param", $file_content, 1);
+				}
+			}
+			elseif ($options == "q"){//quoted
+				if($occurrence !== false){
+					$file_content = preg_replace_nth("/$default/m", "$var\"" . str_replace('"', '\"', $info_param) . "\"", $file_content, $occurrence);
+				}else{
+					$file_content = preg_replace("/$default/m", "$var\"" . str_replace('"', '\"', $info_param) . "\"", $file_content, 1);
+				}
+			}
+			elseif ($options == "sq"){//separated & quoted
+				if($occurrence !== false){
+					$file_content = preg_replace_nth("/$default/m", "$var \"" . str_replace('"','\"',$info_param) . "\"", $file_content, $occurrence);
+				}else{
+					$file_content = preg_replace("/$default/m", "$var \"" . str_replace('"','\"',$info_param) . "\"", $file_content, 1);
+				}
+			}
+			elseif ($options == "sc"){//separated & ending with a comma (used in JC2MP Example)
+				if($occurrence !== false){
+					$file_content = preg_replace_nth("/$default/m", "$var $info_param,", $file_content, $occurrence);
+				}else{
+					$file_content = preg_replace("/$default/m", "$var $info_param,", $file_content, 1);
+				}
+			}
+			elseif ($options == "sqc"){//separated & quoted & ending with a comma
+				if($occurrence !== false){
+					$file_content = preg_replace_nth("/$default/m", "$var \"" . str_replace('"', '\"', $info_param) . "\",", $file_content, $occurrence);
+				}else{
+					$file_content = preg_replace("/$default/m", "$var \"" . str_replace('"', '\"', $info_param) . "\",", $file_content, 1);
+				}
+			}
 			elseif ($options == "key-regex")//replace %key% in <var> and use a regular expression
 			{
 				$var = str_replace("%key%", $info_param, $var);
-				$file_content = preg_replace("/$default/m", "$var", $file_content, 1);
+				if($occurrence !== false){
+					$file_content = preg_replace_nth("/$default/m", "$var", $file_content, $occurrence);
+				}else{
+					$file_content = preg_replace("/$default/m", "$var", $file_content, 1);
+				}
+			}
+			else{
+				if($occurrence !== false){
+					$file_content = preg_replace_nth("/$default/m", "$var$info_param", $file_content, $occurrence);
+				}else{
+					$file_content = preg_replace("/$default/m", "$var$info_param", $file_content, 1);
+				}
 			}
-			else
-				$file_content = preg_replace("/$default/m", "$var$info_param", $file_content, 1);
 		}
 	
 		if ( get_magic_quotes_gpc() )