From c1b1beab72e6b4e266ba9036267d4ba9f7a9c907 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Sun, 18 Jun 2023 21:01:59 +0200 Subject: [PATCH] more cleanPgn() cleanup: trim lines, del initial blanks, one space only... --- PgnParser.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/PgnParser.php b/PgnParser.php index 563e03e..767c5b2 100755 --- a/PgnParser.php +++ b/PgnParser.php @@ -89,11 +89,6 @@ class PgnParser */ $c = preg_replace('/"\]\s*([\.0-9]|{)/s', "\"]\n\n$1", $c); - /* remove space before '[' - * This is possible because brackets within comments are protected above - */ - $c = str_replace(" +[", "[", $c); - /* set '\n\n' between non tag line and tag line (between two games) * This is possible because brackets within comments are protected above */ @@ -102,7 +97,15 @@ class PgnParser /* revert brackets within movetext comments */ $c = str_replace("//--SB--//", "[", $c); - /* max 2 consecutive '\n */ + /* Final trimming: + * - remove initial blanks + * - keep only one space + * - trim lines + * - max 2 consecutive '\n' + */ + $c = preg_replace("/^\s+([^\s])/", "$1", $c); + $c = preg_replace("/ +/", " ", $c); + $c = preg_replace("/\s*\n\s*/", "\n", $c); $c = preg_replace("/\n{3,}/s", "\n\n", $c); return $c;