@@ -124,9 +124,18 @@ private function prepareText($txt) {
124124 */
125125
126126 private function prepareTemplate ($ templateTxt ) {
127- $ templateTxt = preg_replace ('/{%(.*)%}/U ' , '(?<$1>.*) ' , $ templateTxt ); //Replace all {Var} notation to (?<Var>.*) for regex pattern
128- $ templateTxt = str_replace (array ('| ' ,"' " ), array ('\| ' ,"\' " ), $ templateTxt ); //Escape buggy characters
129- $ templateTxt = preg_replace ('/\s+/ ' , ' ' , $ templateTxt ); //Remove all multiple whitespaces and replace it with single space
127+ $ patterns = [
128+ '/ \\\{%(.*)% \\\}/U ' , // 1 Replace all {%Var%}...
129+ '/\s+/ ' , // 2 Replace all white-spaces...
130+ ];
131+
132+ $ replacements = [
133+ '(?<$1>.*) ' , // 1 ...with (?<Var>.*)
134+ ' ' , // 2 ...with a single space
135+ ];
136+
137+ $ templateTxt = preg_replace ($ patterns , $ replacements , preg_quote ($ templateTxt , '/ ' ));
138+
130139 return trim ($ templateTxt );
131140 }
132141
@@ -142,7 +151,7 @@ private function prepareTemplate($templateTxt) {
142151
143152 private function extractData ($ text , $ template ) {
144153 //Extract the text based on the provided template using REGEX
145- preg_match (" | " . $ template . " |s " , $ text , $ matches );
154+ preg_match (' / ' . $ template . ' /s ' , $ text , $ matches );
146155 //Extract only the named parameters from the matched regex array
147156 $ keys = array_filter (array_keys ($ matches ), 'is_string ' );
148157 $ matches = array_intersect_key ($ matches , array_flip ($ keys ));
0 commit comments