3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo 'Trim blank character' . "\n"; var_dump(trim('   f   ')); echo "\n\nTrim blank character via chrs function \n"; var_dump(trim_blank_chrs('   f   ')); echo "\n\nTrim normal space \n"; var_dump(trim(' f ')); /** * More robust version of PHP's trim() function. It includes a list of UTF-8 blank characters * from http://kb.mozillazine.org/Network.IDN.blacklist_chars * * @param string $string The string to trim from * @param string $charlist Optional. The stripped characters can also be specified using the charlist parameter * @return string The trimmed string */ function trim_blank_chrs($string, $charlist="") { $hex_chrs = array( 0x09 => 1, // \x{0009} 0x0A => 1, // \x{000A} 0x0B => 1, // \x{000B} 0x0D => 1, // \x{000D} 0x20 => 1, // \x{0020} 0xC2 => array(0x81 => 1, 0x8D => 1, 0x90 => 1, 0x9D => 1, 0xA0 => 1, 0xAD => 1), // \x{0081}, \x{008D}, \x{0090}, \x{009D}, \x{00A0}, \x{00AD} 0xCC => array(0xB7 => 1, 0xB8 => 1), // \x{0337}, \x{0338} 0xE1 => array(0x85 => array(0x9F => 1, 0xA0 => 1), 0x9A => array(0x80 => 1), 0xA0 => array(0x8E => 1)), // \x{115F}, \x{1160}, \x{1680}, \x{180E} 0xE2 => array(0x80 => array(0x80 => 1, 0x81 => 1, 0x82 => 1, 0x83 => 1, 0x84 => 1, 0x85 => 1, 0x86 => 1, 0x87 => 1, 0x88 => 1, 0x89 => 1, 0x8A => 1, 0x8B => 1, 0x8C => 1, 0x8D => 1, 0x8E => 1, 0x8F => 1, // \x{2000} - \x{200F} 0xA8 => 1, 0xA9 => 1, 0xAA => 1, 0xAB => 1, 0xAC => 1, 0xAD => 1, 0xAE => 1, 0xAF => 1), // \x{2028} - \x{202F} 0x81 => array(0x9F => 1)), // \x{205F} 0xE3 => array(0x80 => array(0x80 => 1), // \x{3000} 0x85 => array(0xA4 => 1)), // \x{3164} 0xEF => array(0xBB => array(0xBF => 1), // \x{FEFF} 0xBE => array(0xA0 => 1), // \x{FFA0} 0xBF => array(0xB9 => 1, 0xBA => 1, 0xBB => 1)), // \x{FFF9} - \x{FFFB} ); $hex_chrs_rev = array( 0x09 => 1, // \x{0009} 0x0A => 1, // \x{000A} 0x0B => 1, // \x{000B} 0x0D => 1, // \x{000D} 0x20 => 1, // \x{0020} 0x81 => array(0xC2 => 1, 0x80 => array(0xE2 => 1)), // \x{0081}, \x{2001} 0x8D => array(0xC2 => 1, 0x80 => array(0xE2 => 1)), // \x{008D}, \x{200D} 0x90 => array(0xC2 => 1), // \x{0090} 0x9D => array(0xC2 => 1), // \x{009D} 0xA0 => array(0xC2 => 1, 0x85 => array(0xE1 => 1), 0x81 => array(0xE2 => 1), 0xBE => array(0xEF => 1)), // \x{00A0}, \x{1160}, \x{2060}, \x{FFA0} 0xAD => array(0xC2 => 1, 0x80 => array(0xE2 => 1)), // \x{00AD}, \x{202D} 0xB8 => array(0xCC => 1), // \x{0338} 0xB7 => array(0xCC => 1), // \x{0337} 0x9F => array(0x85 => array(0xE1 => 1), 0x81 => array(0xE2 => 1)), // \x{115F}, \x{205F} 0x80 => array(0x9A => array(0xE1 => 1), 0x80 => array(0xE2 => 1, 0xE3 => 1)), // \x{1680}, \x{2000}, \x{3000} 0x8E => array(0xA0 => array(0xE1 => 1), 0x80 => array(0xE2 => 1)), // \x{180E}, \x{200E} 0x82 => array(0x80 => array(0xE2 => 1)), // \x{2002} 0x83 => array(0x80 => array(0xE2 => 1)), // \x{2003} 0x84 => array(0x80 => array(0xE2 => 1)), // \x{2004} 0x85 => array(0x80 => array(0xE2 => 1)), // \x{2005} 0x86 => array(0x80 => array(0xE2 => 1)), // \x{2006} 0x87 => array(0x80 => array(0xE2 => 1)), // \x{2007} 0x88 => array(0x80 => array(0xE2 => 1)), // \x{2008} 0x89 => array(0x80 => array(0xE2 => 1)), // \x{2009} 0x8A => array(0x80 => array(0xE2 => 1)), // \x{200A} 0x8B => array(0x80 => array(0xE2 => 1)), // \x{200B} 0x8C => array(0x80 => array(0xE2 => 1)), // \x{200C} 0x8F => array(0x80 => array(0xE2 => 1)), // \x{200F} 0xA8 => array(0x80 => array(0xE2 => 1)), // \x{2028} 0xA9 => array(0x80 => array(0xE2 => 1)), // \x{2029} 0xAA => array(0x80 => array(0xE2 => 1)), // \x{202A} 0xAB => array(0x80 => array(0xE2 => 1)), // \x{202B} 0xAC => array(0x80 => array(0xE2 => 1)), // \x{202C} 0xAE => array(0x80 => array(0xE2 => 1)), // \x{202E} 0xAF => array(0x80 => array(0xE2 => 1)), // \x{202F} 0xA4 => array(0x85 => array(0xE3 => 1)), // \x{3164} 0xBF => array(0xBB => array(0xEF => 1)), // \x{FEFF} 0xB9 => array(0xBF => array(0xEF => 1)), // \x{FFF9} 0xBA => array(0xBF => array(0xEF => 1)), // \x{FFFA} 0xBB => array(0xBF => array(0xEF => 1)), // \x{FFFB} ); // Start from the beginning and work our way in $i = 0; do { // Check to see if we have matched a first character in our utf-8 array $offset = match_sequence($string, $hex_chrs); if(!$offset) { // If not, then we must have a "good" character and we don't need to do anymore processing break; } $string = substr($string, $offset); } while(++$i); // Start from the end and work our way in $string = strrev($string); $i = 0; do { // Check to see if we have matched a first character in our utf-8 array $offset = match_sequence($string, $hex_chrs_rev); if(!$offset) { // If not, then we must have a "good" character and we don't need to do anymore processing break; } $string = substr($string, $offset); } while(++$i); $string = strrev($string); if($charlist) { $string = trim($string, $charlist); } else { $string = trim($string); } return $string; } /** * Match a sequence * * @param string $string The string to match from * @param array $array The array to match from * @param int $i Number in the string * @param int $n Number of matches * @return int The number matched */ function match_sequence($string, $array, $i=0, $n=0) { if($string === "") { return 0; } $ord = ord($string[$i]); if(array_key_exists($ord, $array)) { $level = $array[$ord]; ++$n; if(is_array($level)) { ++$i; return match_sequence($string, $level, $i, $n); } return $n; } return 0; }

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.5.10.0060.01619.19
8.5.00.0150.00723.10
8.4.150.0020.00114.05
8.4.140.0140.00717.46
8.4.130.0100.01217.89
8.4.120.0090.01220.63
8.4.110.0140.00722.54
8.4.100.0130.00817.68
8.4.90.0090.01017.74
8.4.80.0060.00317.90
8.4.70.0110.01017.98
8.4.60.0110.01017.67
8.4.50.0120.00917.70
8.4.40.0100.01020.56
8.4.30.0120.00622.11
8.4.20.0050.00517.59
8.4.10.0130.00622.15
8.3.280.0130.00818.28
8.3.270.0120.00916.54
8.3.260.0110.00916.45
8.3.250.0070.00619.06
8.3.240.0050.00316.70
8.3.230.0120.00716.74
8.3.220.0100.00919.21
8.3.210.0090.00916.86
8.3.200.0050.00317.04
8.3.190.0080.01216.67
8.3.180.0090.01016.57
8.3.170.0150.00318.72
8.3.160.0060.00318.92
8.3.150.0150.00316.71
8.3.140.0040.00419.03
8.3.130.0080.00018.30
8.3.120.0080.00820.88
8.3.110.0090.00016.85
8.3.100.0030.00518.55
8.3.90.0030.00626.77
8.3.80.0060.00318.31
8.3.70.0130.00617.00
8.3.60.0090.00616.50
8.3.50.0090.00918.53
8.3.40.0090.00920.34
8.3.30.0000.01420.51
8.3.20.0060.00324.18
8.3.10.0050.00224.66
8.3.00.0070.00026.16
8.2.290.0130.00620.84
8.2.280.0090.01016.70
8.2.270.0160.00319.03
8.2.260.0110.00418.21
8.2.250.0040.00417.40
8.2.240.0000.00818.76
8.2.230.0090.00020.94
8.2.220.0030.00624.06
8.2.210.0130.00326.77
8.2.200.0030.00616.75
8.2.190.0040.01116.63
8.2.180.0180.00016.75
8.2.170.0060.00919.02
8.2.160.0110.00422.96
8.2.150.0040.00425.66
8.2.140.0040.00424.66
8.2.130.0060.00326.16
8.2.120.0000.00819.23
8.2.110.0030.00722.13
8.2.100.0060.00320.53
8.2.60.0050.00418.02
8.1.330.0100.00916.63
8.1.320.0100.01118.23
8.1.310.0110.00718.39
8.1.300.0040.00422.10
8.1.290.0090.00030.84
8.1.280.0120.00925.92
8.1.270.0080.00024.66
8.1.260.0050.00326.35
8.1.250.0080.00028.09
8.1.240.0030.00619.07
8.1.230.0000.01118.88

preferences:
22.56 ms | 403 KiB | 5 Q