Skip to content

Commit ed5f206

Browse files
authored
Merge pull request #5 from fredericseiler/patch-1
Parse everything !
2 parents a1393fd + a318473 commit ed5f206

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/TextParserClass.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)