3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ /** * Context specific methods for use in secure output escaping */ class Escaper { /** * Entity Map mapping Unicode codepoints to any available named HTML entities. * * While HTML supports far more named entities, the lowest common denominator * has become HTML5's XML Serialisation which is restricted to the those named * entities that XML supports. Using HTML entities would result in this error: * XML Parsing Error: undefined entity * * @var array */ protected static $htmlNamedEntityMap = array( 34 => 'quot', // quotation mark 38 => 'amp', // ampersand 60 => 'lt', // less-than sign 62 => 'gt', // greater-than sign ); /** * Current encoding for escaping. If not UTF-8, we convert strings from this encoding * pre-escaping and back to this encoding post-escaping. * * @var string */ protected $encoding = 'utf-8'; /** * Holds the value of the special flags passed as second parameter to * htmlspecialchars(). We modify these for PHP 5.4 to take advantage * of the new ENT_SUBSTITUTE flag for correctly dealing with invalid * UTF-8 sequences. * * @var string */ protected $htmlSpecialCharsFlags = ENT_QUOTES; /** * Static Matcher which escapes characters for HTML Attribute contexts * * @var callable */ protected $htmlAttrMatcher; /** * Static Matcher which escapes characters for Javascript contexts * * @var callable */ protected $jsMatcher; /** * Static Matcher which escapes characters for CSS Attribute contexts * * @var callable */ protected $cssMatcher; /** * List of all encoding supported by this class * * @var array */ protected $supportedEncodings = array( 'iso-8859-1', 'iso8859-1', 'iso-8859-5', 'iso8859-5', 'iso-8859-15', 'iso8859-15', 'utf-8', 'cp866', 'ibm866', '866', 'cp1251', 'windows-1251', 'win-1251', '1251', 'cp1252', 'windows-1252', '1252', 'koi8-r', 'koi8-ru', 'koi8r', 'big5', '950', 'gb2312', '936', 'big5-hkscs', 'shift_jis', 'sjis', 'sjis-win', 'cp932', '932', 'euc-jp', 'eucjp', 'eucjp-win', 'macroman' ); /** * Constructor: Single parameter allows setting of global encoding for use by * the current object. If PHP 5.4 is detected, additional ENT_SUBSTITUTE flag * is set for htmlspecialchars() calls. * * @param string $encoding * @throws Exception */ public function __construct($encoding = null) { if ($encoding !== null) { $encoding = (string) $encoding; if ($encoding === '') { throw new Exception( get_class($this) . ' constructor parameter does not allow a blank value' ); } $encoding = strtolower($encoding); if (!in_array($encoding, $this->supportedEncodings)) { throw new Exception( 'Value of \'' . $encoding . '\' passed to ' . get_class($this) . ' constructor parameter is invalid. Provide an encoding supported by htmlspecialchars()' ); } $this->encoding = $encoding; } if (defined('ENT_SUBSTITUTE')) { $this->htmlSpecialCharsFlags|= ENT_SUBSTITUTE; } // set matcher callbacks $this->htmlAttrMatcher = array($this, 'htmlAttrMatcher'); $this->jsMatcher = array($this, 'jsMatcher'); $this->cssMatcher = array($this, 'cssMatcher'); } /** * Return the encoding that all output/input is expected to be encoded in. * * @return string */ public function getEncoding() { return $this->encoding; } /** * Escape a string for the HTML Body context where there are very few characters * of special meaning. Internally this will use htmlspecialchars(). * * @param string $string * @return string */ public function escapeHtml($string) { return htmlspecialchars($string, $this->htmlSpecialCharsFlags, $this->encoding); } /** * Escape a string for the HTML Attribute context. We use an extended set of characters * to escape that are not covered by htmlspecialchars() to cover cases where an attribute * might be unquoted or quoted illegally (e.g. backticks are valid quotes for IE). * * @param string $string * @return string */ public function escapeHtmlAttr($string) { $string = $this->toUtf8($string); if ($string === '' || ctype_digit($string)) { return $string; } $result = preg_replace_callback('/[^a-z0-9,\.\-_]/iSu', $this->htmlAttrMatcher, $string); return $this->fromUtf8($result); } /** * Escape a string for the Javascript context. This does not use json_encode(). An extended * set of characters are escaped beyond ECMAScript's rules for Javascript literal string * escaping in order to prevent misinterpretation of Javascript as HTML leading to the * injection of special characters and entities. The escaping used should be tolerant * of cases where HTML escaping was not applied on top of Javascript escaping correctly. * Backslash escaping is not used as it still leaves the escaped character as-is and so * is not useful in a HTML context. * * @param string $string * @return string */ public function escapeJs($string) { $string = $this->toUtf8($string); if ($string === '' || ctype_digit($string)) { return $string; } $result = preg_replace_callback('/[^a-z0-9,\._]/iSu', $this->jsMatcher, $string); return $this->fromUtf8($result); } /** * Escape a string for the URI or Parameter contexts. This should not be used to escape * an entire URI - only a subcomponent being inserted. The function is a simple proxy * to rawurlencode() which now implements RFC 3986 since PHP 5.3 completely. * * @param string $string * @return string */ public function escapeUrl($string) { return rawurlencode($string); } /** * Escape a string for the CSS context. CSS escaping can be applied to any string being * inserted into CSS and escapes everything except alphanumerics. * * @param string $string * @return string */ public function escapeCss($string) { $string = $this->toUtf8($string); if ($string === '' || ctype_digit($string)) { return $string; } $result = preg_replace_callback('/[^a-z0-9]/iSu', $this->cssMatcher, $string); return $this->fromUtf8($result); } /** * Callback function for preg_replace_callback that applies HTML Attribute * escaping to all matches. * * @param array $matches * @return string */ protected function htmlAttrMatcher($matches) { $chr = $matches[0]; $ord = ord($chr); /** * The following replaces characters undefined in HTML with the * hex entity for the Unicode replacement character. */ if (($ord <= 0x1f && $chr != "\t" && $chr != "\n" && $chr != "\r") || ($ord >= 0x7f && $ord <= 0x9f) ) { return '&#xFFFD;'; } /** * Check if the current character to escape has a name entity we should * replace it with while grabbing the integer value of the character. */ if (strlen($chr) > 1) { $chr = $this->convertEncoding($chr, 'UTF-16BE', 'UTF-8'); } $hex = bin2hex($chr); $ord = hexdec($hex); if (isset(static::$htmlNamedEntityMap[$ord])) { return '&' . static::$htmlNamedEntityMap[$ord] . ';'; } /** * Per OWASP recommendations, we'll use upper hex entities * for any other characters where a named entity does not exist. */ if ($ord > 255) { return sprintf('&#x%04X;', $ord); } return sprintf('&#x%02X;', $ord); } /** * Callback function for preg_replace_callback that applies Javascript * escaping to all matches. * * @param array $matches * @return string */ protected function jsMatcher($matches) { $chr = $matches[0]; if (strlen($chr) == 1) { return sprintf('\\x%02X', ord($chr)); } $chr = $this->convertEncoding($chr, 'UTF-16BE', 'UTF-8'); return sprintf('\\u%04s', strtoupper(bin2hex($chr))); } /** * Callback function for preg_replace_callback that applies CSS * escaping to all matches. * * @param array $matches * @return string */ protected function cssMatcher($matches) { $chr = $matches[0]; if (strlen($chr) == 1) { $ord = ord($chr); } else { $chr = $this->convertEncoding($chr, 'UTF-16BE', 'UTF-8'); $ord = hexdec(bin2hex($chr)); } return sprintf('\\%X ', $ord); } /** * Converts a string to UTF-8 from the base encoding. The base encoding is set via this * class' constructor. * * @param string $string * @throws Exception * @return string */ protected function toUtf8($string) { if ($this->getEncoding() === 'utf-8') { $result = $string; } else { $result = $this->convertEncoding($string, 'UTF-8', $this->getEncoding()); } if (!$this->isUtf8($result)) { throw new Exception( sprintf('String to be escaped was not valid UTF-8 or could not be converted: %s', $result) ); } return $result; } /** * Converts a string from UTF-8 to the base encoding. The base encoding is set via this * class' constructor. * @param string $string * @return string */ protected function fromUtf8($string) { if ($this->getEncoding() === 'utf-8') { return $string; } return $this->convertEncoding($string, $this->getEncoding(), 'UTF-8'); } /** * Checks if a given string appears to be valid UTF-8 or not. * * @param string $string * @return bool */ protected function isUtf8($string) { return ($string === '' || preg_match('/^./su', $string)); } /** * Encoding conversion helper which wraps iconv and mbstring where they exist or throws * and exception where neither is available. * * @param string $string * @param string $to * @param array|string $from * @throws Exception * @return string */ protected function convertEncoding($string, $to, $from) { if (function_exists('iconv')) { $result = iconv($from, $to, $string); } elseif (function_exists('mb_convert_encoding')) { $result = mb_convert_encoding($string, $to, $from); } else { throw new Exception( get_class($this) . ' requires either the iconv or mbstring extension to be installed' . ' when escaping for non UTF-8 strings.' ); } if ($result === false) { return ''; // return non-fatal blank string on encoding errors from users } return $result; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0150.00018.31
8.3.50.0140.00016.52
8.3.40.0080.00818.76
8.3.30.0130.00318.44
8.3.20.0040.00420.43
8.3.10.0030.00523.61
8.3.00.0040.00417.47
8.2.180.0110.01116.63
8.2.170.0070.00722.96
8.2.160.0070.01320.47
8.2.150.0070.00024.18
8.2.140.0000.00824.66
8.2.130.0080.00021.04
8.2.120.0030.00526.35
8.2.110.0050.00521.04
8.2.100.0000.01118.03
8.2.90.0050.00319.28
8.2.80.0060.00317.97
8.2.70.0060.00317.50
8.2.60.0000.01017.93
8.2.50.0060.00318.10
8.2.40.0000.00920.77
8.2.30.0000.00720.89
8.2.20.0020.00518.13
8.2.10.0040.00418.13
8.2.00.0030.00618.12
8.1.280.0170.00025.92
8.1.270.0050.00323.84
8.1.260.0040.00426.35
8.1.250.0050.00228.09
8.1.240.0000.00923.79
8.1.230.0080.00419.28
8.1.220.0030.00617.74
8.1.210.0000.00818.77
8.1.200.0030.00717.36
8.1.190.0050.00317.23
8.1.180.0000.00818.10
8.1.170.0060.00318.84
8.1.160.0000.00719.04
8.1.150.0050.00320.59
8.1.140.0040.00419.45
8.1.130.0030.00318.87
8.1.120.0000.00717.39
8.1.110.0040.00417.31
8.1.100.0050.00317.42
8.1.90.0040.00717.31
8.1.80.0000.00717.28
8.1.70.0040.00417.42
8.1.60.0000.01117.45
8.1.50.0040.00417.44
8.1.40.0060.00317.50
8.1.30.0000.00817.48
8.1.20.0060.00317.63
8.1.10.0000.00917.36
8.1.00.0080.00017.42
8.0.300.0040.00418.77
8.0.290.0040.00416.75
8.0.280.0030.00318.36
8.0.270.0000.00717.13
8.0.260.0030.00316.73
8.0.250.0000.00716.91
8.0.240.0050.00216.81
8.0.230.0040.00416.88
8.0.220.0040.00416.85
8.0.210.0000.00716.77
8.0.200.0030.00316.91
8.0.190.0030.00616.82
8.0.180.0030.00516.89
8.0.170.0070.00016.78
8.0.160.0070.00016.86
8.0.150.0090.00016.86
8.0.140.0040.00416.82
8.0.130.0000.00513.26
8.0.120.0000.00816.85
8.0.110.0000.00716.79
8.0.100.0050.00316.89
8.0.90.0040.00416.93
8.0.80.0030.01316.78
8.0.70.0000.00816.67
8.0.60.0050.00316.68
8.0.50.0050.00216.84
8.0.30.0050.01316.97
8.0.20.0120.00617.40
8.0.10.0000.00717.04
8.0.00.0070.01016.69
7.4.330.0050.00015.55
7.4.320.0050.00316.40
7.4.300.0000.00616.51
7.4.290.0030.00616.55
7.4.280.0080.00016.47
7.4.270.0040.00416.54
7.4.260.0030.00313.26
7.4.250.0000.00816.50
7.4.240.0050.00316.61
7.4.230.0040.00416.57
7.4.220.0140.01116.48
7.4.210.0070.01116.39
7.4.200.0050.00216.64
7.4.160.0110.00516.46
7.4.150.0060.01217.40
7.4.140.0100.01017.86
7.4.130.0130.00716.27
7.4.120.0110.01216.59
7.4.110.0060.01216.36
7.4.100.0030.01416.45
7.4.90.0100.00716.41
7.4.80.0090.00919.39
7.4.70.0100.00716.39
7.4.60.0130.00316.43
7.4.50.0040.01416.55
7.4.40.0090.01316.38
7.4.30.0140.00716.37
7.4.00.0130.00314.60
7.3.330.0000.00513.26
7.3.320.0000.00513.09
7.3.310.0000.00716.18
7.3.300.0000.00816.24
7.3.290.0030.01216.25
7.3.280.0080.00916.23
7.3.270.0070.01117.40
7.3.260.0130.00916.42
7.3.250.0140.00716.43
7.3.240.0130.01016.44
7.3.230.0080.01116.39
7.3.210.0100.00716.18
7.3.200.0100.00716.33
7.3.190.0090.00916.41
7.3.180.0060.00916.19
7.3.170.0030.01416.31
7.3.160.0080.01116.47
7.3.10.0100.01715.96
7.3.00.0100.00416.41
7.2.330.0030.01516.64
7.2.320.0140.00316.67
7.2.310.0070.01116.55
7.2.300.0130.00416.66
7.2.290.0130.00316.31
7.2.130.0120.00916.46
7.2.120.0090.00416.31
7.2.110.0040.00816.41
7.2.100.0030.01216.36
7.2.90.0080.00416.31
7.2.80.0060.00616.58
7.2.70.0100.01016.53
7.2.60.0060.00816.19
7.2.50.0100.00616.48
7.2.40.0030.00916.59
7.2.30.0070.00716.39
7.2.20.0170.00016.58
7.2.10.0030.00616.53
7.2.00.0040.01317.53
7.1.250.0140.00415.41
7.1.200.0060.00915.60
7.1.100.3260.01016.25
7.1.70.0130.00017.53
7.1.60.0060.00917.53
7.1.50.0170.00817.53
7.1.40.0170.00717.53
7.1.30.0130.00817.53
7.1.20.0150.00517.53
7.1.10.0110.00417.54
7.1.00.0130.00617.54
7.0.200.0100.00317.53
7.0.190.0250.00317.53
7.0.180.0100.01017.53
7.0.170.0160.00017.53
7.0.160.0100.01017.54
7.0.150.0070.00717.54
7.0.140.0080.00817.54
7.0.130.0080.00517.54
7.0.120.0070.00517.54
7.0.110.0000.01417.54
7.0.100.0000.01217.54
7.0.90.0060.00617.54
7.0.80.0040.00717.54
7.0.70.0090.00717.54
7.0.60.0070.00417.54
7.0.50.0100.00317.54
7.0.40.0090.00417.54
7.0.30.0080.00417.54
7.0.20.0060.00617.54
7.0.10.0080.00417.54
7.0.00.0040.01517.54
5.6.300.0040.04820.87
5.6.290.0100.04220.71
5.6.280.0090.04321.04
5.6.270.0100.05620.88
5.6.260.0030.04621.03
5.6.250.0130.03620.96
5.6.240.0170.03720.88
5.6.230.0140.04521.08
5.6.220.0110.04620.83
5.6.210.0000.04921.06
5.6.200.0160.03921.02
5.6.190.0090.04220.88
5.6.180.0150.05721.03
5.6.170.0090.05721.10
5.6.160.0070.04320.91
5.6.150.0060.04120.90
5.6.140.0110.03721.08
5.6.130.0100.04020.87
5.6.120.0060.04321.00
5.6.110.0070.04020.86
5.6.100.0030.04521.00
5.6.90.0080.03920.82
5.6.80.0030.04220.38
5.6.70.0040.03920.48
5.6.60.0000.06420.29
5.6.50.0130.03520.37
5.6.40.0030.04420.28
5.6.30.0000.05220.34
5.6.20.0100.03820.27
5.6.10.0130.03520.37
5.6.00.0130.05020.32
5.5.380.0000.04317.58
5.5.370.0070.04017.69
5.5.360.0060.04217.67
5.5.350.0090.04017.67
5.5.340.0060.04218.10
5.5.330.0030.04518.10
5.5.320.0070.04318.00
5.5.310.0100.04317.73
5.5.300.0040.04318.12
5.5.290.0040.04818.05
5.5.280.0030.04117.99
5.5.270.0030.04118.18
5.5.260.0000.04518.05
5.5.250.0170.03617.55
5.5.240.0000.04117.54
5.5.230.0070.04017.54
5.5.220.0040.03917.54
5.5.210.0120.06117.54
5.5.200.0090.03317.54
5.5.190.0030.04617.54
5.5.180.0030.05117.54
5.5.160.0070.03917.54
5.5.150.0030.03817.54
5.5.140.0070.04017.54
5.5.130.0040.05117.54
5.5.120.0030.04017.54
5.5.110.0030.04017.54
5.5.100.0000.04117.54
5.5.90.0030.04017.54
5.5.80.0040.03517.54
5.5.70.0070.03917.54
5.5.60.0120.02817.54
5.5.50.0070.04117.54
5.5.40.0000.04417.54
5.5.30.0030.04517.54
5.5.20.0070.04317.54
5.5.10.0090.03417.54
5.5.00.0000.04117.54
5.4.450.0060.04119.36
5.4.440.0040.04219.44
5.4.430.0070.04019.46
5.4.420.0070.03719.21
5.4.410.0030.04319.33
5.4.400.0070.03719.06
5.4.390.0030.03918.98
5.4.380.0100.03718.99
5.4.370.0030.04119.13
5.4.360.0040.04018.89
5.4.350.0090.03118.89
5.4.340.0000.04119.20
5.4.320.0040.03918.97
5.4.310.0060.03919.11
5.4.300.0030.04318.97
5.4.290.0150.04519.20
5.4.280.0030.03819.13
5.4.270.0090.04319.20
5.4.260.0070.04119.24
5.4.250.0000.04119.13
5.4.240.0000.04019.05
5.4.230.0000.04619.04
5.4.220.0100.03619.04
5.4.210.0030.03819.08
5.4.200.0070.03919.08
5.4.190.0060.04118.91
5.4.180.0100.03719.13
5.4.170.0060.03819.20
5.4.160.0000.04919.19
5.4.150.0000.04619.06
5.4.140.0030.04217.54
5.4.130.0030.03617.54
5.4.120.0030.06417.54
5.4.110.0030.04117.54
5.4.100.0060.03517.54
5.4.90.0030.03717.54
5.4.80.0000.03717.54
5.4.70.0060.03117.54
5.4.60.0060.03417.54
5.4.50.0060.03717.54
5.4.40.0000.04217.54
5.4.30.0030.03517.54
5.4.20.0030.03417.54
5.4.10.0000.04017.54
5.4.00.0100.02917.54
5.3.290.0060.03517.54
5.3.280.0040.04317.54
5.3.270.0130.03517.54
5.3.260.0030.03717.54
5.3.250.0060.03517.54
5.3.240.0030.04017.54
5.3.230.0000.04017.54
5.3.220.0030.05217.54
5.3.210.0060.03517.54
5.3.200.0000.04617.54
5.3.190.0030.05717.54
5.3.180.0100.03117.54
5.3.170.0070.03417.54
5.3.160.0000.05017.54
5.3.150.0030.04017.54
5.3.140.0030.04217.54
5.3.130.0090.04417.54
5.3.120.0070.03717.54
5.3.110.0000.04217.54
5.3.100.0030.03817.54
5.3.90.0070.03017.54
5.3.80.0000.03917.54
5.3.70.0060.03217.54
5.3.60.0030.03817.54
5.3.50.0000.03817.54
5.3.40.0040.03317.54
5.3.30.0070.03417.54
5.3.20.0030.03417.54
5.3.10.0070.03417.54
5.3.00.0060.05817.54
5.2.170.0030.02817.54
5.2.160.0000.04117.54
5.2.150.0000.03717.54
5.2.140.0170.03417.54
5.2.130.0000.03417.54
5.2.120.0000.03617.54
5.2.110.0000.03017.54
5.2.100.0000.03317.54
5.2.90.0060.03617.54
5.2.80.0000.04117.54
5.2.70.0070.03017.54
5.2.60.0000.03417.54
5.2.50.0040.03217.54
5.2.40.0070.03017.54
5.2.30.0040.03517.54
5.2.20.0040.02817.54
5.2.10.0040.03217.54
5.2.00.0040.03217.54
5.1.60.0030.02917.54
5.1.50.0030.02617.54
5.1.40.0090.02417.54
5.1.30.0030.03017.54
5.1.20.0000.03217.54
5.1.10.0000.02917.54
5.1.00.0060.02517.54
5.0.50.0060.01617.54
5.0.40.0000.02317.54
5.0.30.0030.03417.54
5.0.20.0150.01817.54
5.0.10.0160.02317.54
5.0.00.0000.03317.54
4.4.90.0000.01917.54
4.4.80.0040.01517.54
4.4.70.0000.01617.54
4.4.60.0030.01517.54
4.4.50.0000.02017.54
4.4.40.0040.02617.54
4.4.30.0040.01517.54
4.4.20.0030.01817.54
4.4.10.0030.01717.54
4.4.00.0000.02617.54
4.3.110.0040.01617.54
4.3.100.0000.01717.54
4.3.90.0000.01817.54
4.3.80.0040.02617.54
4.3.70.0000.01717.54
4.3.60.0000.01717.54
4.3.50.0040.01817.54
4.3.40.0000.02417.54
4.3.30.0000.01917.54
4.3.20.0000.02117.54
4.3.10.0030.01417.54
4.3.00.0030.01617.54

preferences:
63.29 ms | 400 KiB | 5 Q