From 2e4afde68dae7f410af8c9e3a0664fcf97ce8a3d Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Fri, 5 May 2017 21:55:58 +0100 Subject: [PATCH] faster check substr at beginning of string --- Parsedown.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 6ef12d3..096c399 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1533,7 +1533,7 @@ class Parsedown unset($Element['attributes'][$att]); } # dump onevent attribute - elseif (preg_match('/^on/i', $att)) + elseif (self::striAtStart($att, 'on')) { unset($Element['attributes'][$att]); } @@ -1551,7 +1551,7 @@ class Parsedown foreach ($this->safeLinksWhitelist as $scheme) { - if (stripos($Element['attributes'][$attribute], $scheme) === 0) + if (self::striAtStart($Element['attributes'][$attribute], $scheme)) { $safe = true; @@ -1584,6 +1584,20 @@ class Parsedown return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8'); } + protected static function striAtStart($string, $needle) + { + $len = strlen($needle); + + if ($len > strlen($string)) + { + return false; + } + else + { + return strtolower(substr($string, 0, $len)) === strtolower($needle); + } + } + static function instance($name = 'default') { if (isset(self::$instances[$name]))