3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Security\Sanitize;class SanitizeInput { protected $strCode = '', $arrTokens = array(); public static $arrInvalidTokens = array( T_BAD_CHARACTER, T_ENDDECLARE, T_END_HEREDOC, T_EVAL, T_FILE, T_GLOBAL, T_HALT_COMPILER, T_GOTO, T_IMPLEMENTS, T_INCLUDE, T_INCLUDE_ONCE, T_LINE, T_PRIVATE, T_PUBLIC, T_PROTECTED, T_REQUIRE, T_REQUIRE_ONCE, T_STRING_VARNAME, T_YIELD, ); public function __construct($arrExtraInvalidTokens = array()) { if(!function_exists('token_get_all') || !function_exists('token_name')) { throw new \Exception(__CLASS__ . ' requirements not met. "token_get_all" and "token_name" functions are required'); return new \stdClass(); } if(is_array($arrExtraInvalidTokens) && !empty($arrExtraInvalidTokens)) { foreach($arrExtraInvalidTokens as $intInvalidToken) { self::$arrInvalidTokens[] = $intInvalidToken; } } } protected function getRegexPatternFromToken($intToken) { $strOut = ''; switch(token_name($intToken)) { case 'T_ABSTRACT' : { $strOut .= '(abstract)+/i'; break; } case 'T_AND_EQUAL': { $strOut .= '([\&\=])+'; break; } case 'T_ARRAY': { $strOut .= '(array)+/i'; } case 'T_ARRAY_CAST' : { $strOut .= '(\(array\)/i)+'; } case 'T_AS': { $strOut .= '(as)+/i'; break; } case 'T_BAD_CHARACTER' : { $strOut .= '([\x00-\x32])+'; break; } case 'T_WHITESPACE' : { $strOut .= '(\s|\t)+'; break; } } return $strOut; } protected function stripTokenValues() { $strOut = ''; $arrTokens = $this->arrTokens; foreach($arrTokens as $arrToken) { foreach($arrToken as $arrTokenDetails) { if(!empty($arrTokenDetails[0]) && is_long($arrTokenDetails[0])) { if(in_array($arrTokenDetails[0], self::$arrInvalidTokens)) { $strPattern = $this->getRegexPatternFromToken($arrTokenDetails[0]); /** @warning Assignment in check */ if(($strOut = preg_replace('/' . $strPattern . '/', '', $this->strCode))) { continue; } else { throw new \Exception('Could not fix patterns matched in template'); } } } } } $this->strCode = $strOut; return $strOut; } protected function findTokens() { $arrOut = array(); $arrTokens = token_get_all($this->strCode); // Ensure we have an array of tokens if(is_array($arrTokens)) { $this->arrTokens[] = $arrTokens; } return $arrTokens; } // Need to fix the second parameter functionality public function secureInput($strCode = '', $blIsFile = false) { $strOut = ''; // Check if we are using a file or plain input if($blIsFile) { // Ensure the file exists and is readable if(file_exists($strCode) && is_readable($strCode)) { $this->strCode = file_get_contents($strCode); } } else { $this->strCode = $strCode; } if(!(empty($this->findTokens()))) { $strOut = $this->stripTokenValues(); } return $strOut; }}// $x = new SanitizeInput();$x = new SanitizeInput(array(T_WHITESPACE));try { var_dump($x->secureInput(file_get_contents('/home/rclifford/Desktop/tokenizer_test.php')));} catch(\Exception $e) { var_dump($e->getMessage());}<?php

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)
5.4.310.0070.03812.53
5.4.300.0060.04012.53
5.4.290.0050.05012.53
5.4.280.0070.04012.43
5.4.270.0090.04312.43
5.4.260.0080.04112.43
5.4.250.0050.04012.43
5.4.240.0060.03512.43
5.4.230.0040.04112.42
5.4.220.0100.03612.42
5.4.210.0100.03212.42
5.4.200.0080.03712.42
5.4.190.0040.04112.41
5.4.180.0050.04812.42
5.4.170.0030.03812.43
5.4.160.0070.03412.43
5.4.150.0040.04112.42
5.4.140.0050.03912.11
5.4.130.0040.04012.09
5.4.120.0050.03612.05
5.4.110.0050.03612.05
5.4.100.0070.04212.05
5.4.90.0040.04912.05
5.4.80.0050.03812.05
5.4.70.0080.03712.05
5.4.60.0070.03712.04
5.4.50.0050.04612.04
5.4.40.0090.03512.03
5.4.30.0090.04512.03
5.4.20.0040.03712.03
5.4.10.0060.04212.03
5.4.00.0050.03711.52
5.3.280.0030.04012.73
5.3.270.0030.04312.74
5.3.260.0100.04412.74
5.3.250.0040.03812.73
5.3.240.0040.03812.73
5.3.230.0050.03912.72
5.3.220.0060.03812.70
5.3.210.0060.03812.70
5.3.200.0050.03612.69
5.3.190.0050.03812.70
5.3.180.0040.03812.69
5.3.170.0050.03712.70
5.3.160.0050.03712.69
5.3.150.0040.04012.70
5.3.140.0040.04012.69
5.3.130.0060.03812.68
5.3.120.0050.04312.68
5.3.110.0020.04312.68
5.3.100.0030.03912.16
5.3.90.0080.04212.14
5.3.80.0070.03512.13
5.3.70.0040.03912.13
5.3.60.0070.04412.12
5.3.50.0090.04212.06
5.3.40.0080.03512.06
5.3.30.0050.03512.03
5.3.20.0040.03711.80
5.3.10.0080.03211.78
5.3.00.0060.03511.75
5.2.170.0040.0309.18
5.2.160.0070.0279.19
5.2.150.0040.0319.19
5.2.140.0060.0299.18
5.2.130.0080.0299.14
5.2.120.0030.0289.14
5.2.110.0080.0259.15
5.2.100.0050.0279.14
5.2.90.0040.0299.14
5.2.80.0050.0299.14
5.2.70.0070.0319.14
5.2.60.0050.0299.10
5.2.50.0090.0329.06
5.2.40.0040.0299.04
5.2.30.0030.0319.02
5.2.20.0030.0339.00
5.2.10.0060.0268.93
5.2.00.0060.0308.79
5.1.60.0060.0278.07
5.1.50.0060.0238.07
5.1.40.0030.0258.05
5.1.30.0070.0278.40
5.1.20.0040.0278.42
5.1.10.0020.0288.14
5.1.00.0010.0288.15
5.0.50.0030.0226.63
5.0.40.0020.0216.48
5.0.30.0030.0316.30
5.0.20.0050.0186.27
5.0.10.0030.0206.25
5.0.00.0030.0306.23
4.4.90.0030.0154.78
4.4.80.0020.0174.76
4.4.70.0020.0164.75
4.4.60.0030.0154.75
4.4.50.0020.0194.77
4.4.40.0080.0224.71
4.4.30.0030.0164.76
4.4.20.0030.0154.84
4.4.10.0010.0174.85
4.4.00.0020.0254.76
4.3.110.0010.0174.67
4.3.100.0010.0164.67
4.3.90.0030.0174.63
4.3.80.0030.0234.58
4.3.70.0020.0164.63
4.3.60.0030.0144.62
4.3.50.0030.0144.62
4.3.40.0040.0224.53
4.3.30.0040.0193.29
4.3.20.0030.0153.26
4.3.10.0020.0153.22
4.3.00.0100.01314.09

preferences:
141.08 ms | 1394 KiB | 7 Q