3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Html{ protected $reachedLimit = false, $totalLen = 0, $maxLen = 25, $toRemove = array(); public static function trim($html, $maxLen = 25){ $dom = new DomDocument(); $dom->loadHTML($html); $html = new static(); $toRemove = $html->walk($dom, $maxLen); // remove any nodes that passed our limit foreach($toRemove as $child) $child->parentNode->removeChild($child); // remove wrapper tags added by DD (doctype, html...) if(version_compare(PHP_VERSION, '5.3.6') < 0){ // http://stackoverflow.com/a/6953808/1058140 $dom->removeChild($dom->firstChild); $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); return $dom->saveHTML(); } return $dom->saveHTML($dom->getElementsByTagName('body')->item(0)); } protected function walk(DomNode $node, $maxLen){ if($this->reachedLimit){ $this->toRemove[] = $node; }else{ // only text nodes should have text, // so do the splitting here if($node instanceof DomText){ $this->totalLen += $nodeLen = strlen($node->nodeValue); // use mb_strlen / mb_substr for UTF-8 support if($this->totalLen > $maxLen){ $node->nodeValue = substr($node->nodeValue, 0, $nodeLen - ($this->totalLen - $maxLen)) . '...'; $this->reachedLimit = true; } } // if node has children, walk its child elements if(isset($node->childNodes)) foreach($node->childNodes as $child) $this->walk($child, $maxLen); } return $this->toRemove; } } print html::trim('<p>I can haz <strong> html trim </strong>', 4);

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.40.0080.00819.95
8.3.30.0120.00619.20
8.2.170.0070.01819.34
8.2.160.0070.01020.22
7.4.10.0180.01416.94
7.4.00.0060.01515.79
7.3.130.0070.02416.48
7.3.120.0140.00815.32
7.3.110.0100.01215.79
7.3.100.0140.00715.70
7.3.90.0120.00815.76
7.3.80.0110.00915.79
7.3.70.0140.00515.73
7.3.60.0210.00515.75
7.3.50.0090.01115.59
7.3.40.0110.01115.86
7.3.30.0120.01115.78
7.3.20.0180.00617.50
7.3.10.0120.00817.38
7.3.00.0110.00817.40
7.2.260.0160.01316.91
7.2.250.0090.01116.07
7.2.240.0150.01015.95
7.2.230.0160.00816.07
7.2.220.0130.01116.02
7.2.210.0090.01115.99
7.2.200.0170.00315.97
7.2.190.0150.00815.90
7.2.180.0090.01015.90
7.2.170.0140.00716.11
7.2.160.0100.01716.99
7.2.150.0170.01418.72
7.2.140.0150.01216.87
7.2.130.0130.01117.84
7.2.120.0380.01016.88
7.2.110.0130.00617.07
7.2.100.0150.00717.80
7.2.90.0100.01216.16
7.2.80.0110.00915.55
7.2.70.0150.00714.95
7.2.60.0110.00815.21
7.2.50.0350.01316.74
7.2.40.0160.01017.89
7.2.30.0130.01217.76
7.2.20.0170.00716.97
7.2.10.0140.00917.77
7.2.00.0130.00817.82
7.1.330.0130.00715.90
7.1.320.0280.00715.98
7.1.310.0190.01215.92
7.1.300.1050.01415.77
7.1.290.1400.00215.69
7.1.280.0200.00915.99
7.1.270.1120.01015.73
7.1.260.0290.01015.74
7.1.250.0310.01115.88
7.1.240.0090.01517.62
7.1.230.0110.01117.47
7.1.220.0210.00817.49
7.1.210.0300.00417.64
7.1.200.0240.00915.84
7.1.190.0640.01115.82
7.1.180.0120.02015.71
7.1.170.0210.00917.61
7.1.160.0600.01115.82
7.1.150.0480.00617.40
7.1.140.0240.00716.01
7.1.130.0200.00815.97
7.1.120.0190.01117.61
7.1.110.0620.01315.66
7.1.100.0220.00615.54
7.1.90.0220.00817.66
7.1.80.0650.00715.86
7.1.70.0160.00715.23
7.1.60.0110.01318.35
7.1.50.0100.01216.53
7.1.40.0210.00713.13
7.1.30.0260.00715.94
7.1.20.0470.00913.13
7.1.10.0220.00817.67
7.1.00.0140.02319.15
7.0.330.0180.00817.41
7.0.320.0180.01017.35
7.0.310.0310.00517.22
7.0.300.0210.00917.32
7.0.290.0210.00617.08
7.0.280.0440.01015.46
7.0.270.0530.01015.32
7.0.260.0130.01117.23
7.0.250.0180.01115.45
7.0.240.0100.02017.19
7.0.230.0170.00617.36
7.0.220.0380.00715.32
7.0.210.0160.00913.13
7.0.200.0110.00917.10
7.0.190.0800.00613.13
7.0.180.0210.00917.22
7.0.170.0200.01015.67
7.0.160.0250.00817.05
7.0.150.0810.00415.40
7.0.140.0200.04118.72
7.0.130.0490.01015.32
7.0.120.0300.00017.18
7.0.110.3340.00715.37
7.0.100.0910.03017.83
7.0.90.0280.02517.81
7.0.80.0340.04017.71
7.0.70.0370.02717.81
7.0.60.0150.04318.59
7.0.50.0320.04817.98
7.0.40.0110.02816.62
7.0.30.0120.03916.55
7.0.20.0170.04117.72
7.0.10.0090.04817.72
7.0.00.0140.03917.72
5.6.400.0200.00916.01
5.6.390.0190.01116.40
5.6.380.0130.01716.37
5.6.370.0140.01616.37
5.6.360.0180.01216.31
5.6.350.0110.01516.23
5.6.340.0220.01516.45
5.6.330.0190.00413.13
5.6.320.0090.00013.13
5.6.310.0190.00013.13
5.6.300.0180.01416.08
5.6.290.0240.00716.43
5.6.280.0140.03818.66
5.6.270.0200.01016.05
5.6.260.0230.00816.32
5.6.250.0110.03018.60
5.6.240.0080.05318.45
5.6.230.0160.02818.55
5.6.220.0220.04018.45
5.6.210.0080.05218.51
5.6.200.0150.04218.54
5.6.190.0160.04518.71
5.6.180.0120.05018.65
5.6.170.0110.03318.82
5.6.160.0160.04218.62
5.6.150.0130.03918.71
5.6.140.0170.04118.67
5.6.130.0120.04918.57
5.6.120.0160.04218.72
5.6.110.0100.04518.55
5.6.100.0090.05718.65
5.6.90.0170.04418.69
5.6.80.0130.02818.25
5.6.70.0140.02518.40
5.6.60.0090.04018.32
5.6.50.0150.04318.46
5.6.40.0070.05018.24
5.6.30.0180.03618.28
5.6.20.0120.04618.41
5.6.10.0150.04318.23
5.6.00.0170.04318.21
5.5.380.0110.03818.44
5.5.370.0130.03218.36
5.5.360.0160.03818.30
5.5.350.0180.02818.29
5.5.340.0160.04418.65
5.5.330.0140.04218.44
5.5.320.0080.05418.62
5.5.310.0090.02718.54
5.5.300.0060.05018.72
5.5.290.0120.03418.63
5.5.280.0120.04518.60
5.5.270.0100.03318.51
5.5.260.0150.04718.51
5.5.250.0100.05518.47
5.5.240.0170.02518.29
5.5.230.0060.03018.19
5.5.220.0150.04018.22
5.5.210.0130.04318.26
5.5.200.0150.04018.27
5.5.190.0140.04218.30
5.5.180.0120.04318.21
5.5.170.0150.01215.98
5.5.160.0080.03718.32
5.5.150.0090.02618.15
5.5.140.0090.02618.25
5.5.130.0070.02918.20
5.5.120.0160.04318.25
5.5.110.0110.04618.04
5.5.100.0090.03518.21
5.5.90.0130.02918.21
5.5.80.0120.02218.06
5.5.70.0160.03818.10
5.5.60.0080.04818.22
5.5.50.0120.02618.12
5.5.40.0130.04618.24
5.5.30.0160.02218.20
5.5.20.0130.03818.22
5.5.10.0090.03918.07
5.5.00.0150.03418.09
5.4.450.0130.04216.31
5.4.440.0090.05016.35
5.4.430.0170.04316.34
5.4.420.0110.04416.33
5.4.410.0100.04216.34
5.4.400.0100.03416.15
5.4.390.0060.05116.21
5.4.380.0110.03416.16
5.4.370.0100.03416.12
5.4.360.0040.05016.16
5.4.350.0140.03616.18
5.4.340.0100.02516.16
5.4.330.0140.00513.36
5.4.320.0110.02916.21
5.4.310.0110.04116.25
5.4.300.0150.01916.09
5.4.290.0070.02716.07
5.4.280.0090.04316.12
5.4.270.0060.04116.07
5.4.260.0080.04116.15
5.4.250.0080.03016.17
5.4.240.0070.04316.17
5.4.230.0090.04216.04
5.4.220.0090.02516.20
5.4.210.0070.04416.12
5.4.200.0070.04316.07
5.4.190.0130.03816.16
5.4.180.0140.01816.17
5.4.170.0110.04116.11
5.4.160.0140.03816.05
5.4.150.0090.03416.01
5.4.140.0100.03014.82
5.4.130.0080.03314.85
5.4.120.0050.02914.80
5.4.110.0080.03314.93
5.4.100.0090.02514.84
5.4.90.0060.04114.90
5.4.80.0080.02714.77
5.4.70.0060.04214.79
5.4.60.0100.03814.87
5.4.50.0080.03214.92
5.4.40.0110.03514.88
5.4.30.0120.03514.78
5.4.20.0110.03414.83
5.4.10.0100.04114.81
5.4.00.0080.04114.57
5.3.290.0130.03614.15
5.3.280.0090.03813.95
5.3.270.0120.04014.17
5.3.260.0090.02614.03
5.3.250.0090.02614.06
5.3.240.0060.03814.03
5.3.230.0100.04113.99
5.3.220.0070.03013.98
5.3.210.0070.04514.14
5.3.200.0110.03314.00
5.3.190.0040.03413.99
5.3.180.0030.04814.04
5.3.170.0130.03913.95
5.3.160.0110.03414.16
5.3.150.0100.04214.00
5.3.140.0080.03813.98
5.3.130.0080.03513.95
5.3.120.0110.03113.94
5.3.110.0080.03914.21
5.3.100.0100.03713.94
5.3.90.0090.02713.74
5.3.80.0100.02013.64
5.3.70.0060.03313.71
5.3.60.0070.02513.72
5.3.50.0040.02613.67
5.3.40.0090.03113.70
5.3.30.0080.02213.85
5.3.20.0080.02013.58
5.3.10.0090.03013.50
5.3.00.0110.03513.42
5.2.170.0000.07011.77
5.2.160.0000.04711.77
5.2.150.0000.04011.77
5.2.140.0030.03311.77
5.2.130.0030.03311.77
5.2.120.0030.03311.77
5.2.110.0000.03011.77
5.2.100.0130.03711.77
5.2.90.0070.03711.77
5.2.80.0000.05311.77
5.2.70.0000.05711.77
5.2.60.0000.03011.77
5.2.50.0000.05711.77
5.2.40.0030.05311.77
5.2.30.0100.02011.77
5.2.20.0070.04711.77
5.2.10.0030.05711.77
5.2.00.0030.03711.77
5.1.60.0100.04711.77
5.1.50.0000.03011.77
5.1.40.0000.03311.77
5.1.30.0000.05711.77
5.1.20.0070.02311.77
5.1.10.0000.03711.77
5.1.00.0030.05311.77
5.0.50.0000.04011.77
5.0.40.0030.03011.77
5.0.30.0030.05711.77
5.0.20.0100.04011.77
5.0.10.0000.03311.77
5.0.00.0000.05711.77
4.4.90.0030.01311.77
4.4.80.0030.03311.77
4.4.70.0070.03011.77
4.4.60.0070.02711.77
4.4.50.0030.03711.77
4.4.40.0070.04711.77
4.4.30.0030.01311.77
4.4.20.0000.03311.77
4.4.10.0000.03311.77
4.4.00.0000.05711.77
4.3.110.0030.03011.77
4.3.100.0000.03311.77
4.3.90.0070.03011.77
4.3.80.0030.02711.77
4.3.70.0000.03011.77
4.3.60.0070.03011.77
4.3.50.0000.02011.77
4.3.40.0030.04311.77
4.3.30.0030.01711.77
4.3.20.0000.03711.77
4.3.10.0000.02311.77
4.3.00.0030.01711.77

preferences:
39.49 ms | 400 KiB | 5 Q