Minor improvements to php highlighter
authorAlexander Ebert <ebert@woltlab.com>
Fri, 4 Dec 2015 17:42:39 +0000 (18:42 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 4 Dec 2015 17:42:39 +0000 (18:42 +0100)
wcfsetup/install/files/lib/system/bbcode/highlighter/PhpHighlighter.class.php

index 349be299ab35126abde9a4eed6edcb026d43932a..9ccdec5bc1b90e7639e1f21bc6566a555930bfcf 100644 (file)
@@ -49,8 +49,17 @@ class PhpHighlighter extends Highlighter {
                
                // remove added php tags
                if ($phpTagsAdded) {
-                       $regex = new Regex('([^\\2]*)(&lt;\?php&nbsp;)(.*)(&nbsp;.*\?&gt;)([^\\4]*)', Regex::CASE_INSENSITIVE | Regex::DOT_ALL);
-                       $highlightedCode = $regex->replace($highlightedCode, '\\1\\3\\5');
+                       // the opening and closing PHP tags were added previously, hence we actually do
+                       // know that the first (last for the closing tag) occurence is the one inserted
+                       // by us. The previously used regex was bad because it was significantly slower
+                       // and could easily hit the backtrace limit for larger inputs
+                       $openingTag = mb_strpos($highlightedCode, '&lt;?php&nbsp;');
+                       $closingTag = mb_strrpos($highlightedCode, '?&gt;');
+                       $tmp = mb_substr($highlightedCode, 0, $openingTag);
+                       $tmp .= mb_substr($highlightedCode, $openingTag + 14, $closingTag - $openingTag - 14);
+                       $tmp .= mb_substr($highlightedCode, $closingTag + 5);
+                       
+                       $highlightedCode = $tmp;
                }
                
                // remove breaks