From a3184738c09d43eca6439ce4c140b8a59971d1d7 Mon Sep 17 00:00:00 2001 From: Frederic Seiler Date: Tue, 23 Aug 2016 15:17:19 +0200 Subject: [PATCH] Parse everything ! Use preg_quote to allow parsing special chars. --- src/TextParserClass.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/TextParserClass.php b/src/TextParserClass.php index e2ec6cb..b0df605 100644 --- a/src/TextParserClass.php +++ b/src/TextParserClass.php @@ -124,9 +124,18 @@ private function prepareText($txt) { */ private function prepareTemplate($templateTxt) { - $templateTxt = preg_replace('/{%(.*)%}/U', '(?<$1>.*)', $templateTxt); //Replace all {Var} notation to (?.*) for regex pattern - $templateTxt = str_replace(array('|',"'"), array('\|',"\'"), $templateTxt); //Escape buggy characters - $templateTxt = preg_replace('/\s+/', ' ', $templateTxt); //Remove all multiple whitespaces and replace it with single space + $patterns = [ + '/\\\{%(.*)%\\\}/U', // 1 Replace all {%Var%}... + '/\s+/', // 2 Replace all white-spaces... + ]; + + $replacements = [ + '(?<$1>.*)', // 1 ...with (?.*) + ' ', // 2 ...with a single space + ]; + + $templateTxt = preg_replace($patterns, $replacements, preg_quote($templateTxt, '/')); + return trim($templateTxt); } @@ -142,7 +151,7 @@ private function prepareTemplate($templateTxt) { private function extractData($text, $template) { //Extract the text based on the provided template using REGEX - preg_match("|" . $template . "|s", $text, $matches); + preg_match('/' . $template . '/s', $text, $matches); //Extract only the named parameters from the matched regex array $keys = array_filter(array_keys($matches), 'is_string'); $matches = array_intersect_key($matches, array_flip($keys));