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>', 14);

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.0060.00916.88
8.3.50.0050.00922.22
8.3.40.0140.00018.98
8.3.30.0080.00619.27
8.3.20.0000.00820.38
8.3.10.0070.00023.86
8.3.00.0080.00019.93
8.2.180.0080.00818.91
8.2.170.0160.00722.96
8.2.160.0110.00319.59
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0050.00320.04
8.2.110.0030.00721.14
8.2.100.0060.00618.15
8.2.90.0050.00319.51
8.2.80.0040.00417.97
8.2.70.0000.00818.00
8.2.60.0080.00017.90
8.2.50.0000.00918.41
8.2.40.0040.00420.72
8.2.30.0040.00419.61
8.2.20.0040.00417.93
8.2.10.0070.00018.46
8.2.00.0040.00418.02
8.1.280.0040.01125.92
8.1.270.0080.00024.22
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0060.00322.49
8.1.230.0060.00618.07
8.1.220.0040.00417.90
8.1.210.0060.00218.77
8.1.200.0080.00317.73
8.1.190.0030.00617.48
8.1.180.0030.00518.10
8.1.170.0050.00318.81
8.1.160.0000.00919.16
8.1.150.0040.00420.41
8.1.140.0040.00417.62
8.1.130.0040.00418.04
8.1.120.0040.00417.64
8.1.110.0030.00517.64
8.1.100.0000.00717.71
8.1.90.0040.00417.66
8.1.80.0000.00717.66
8.1.70.0050.00317.64
8.1.60.0030.00617.90
8.1.50.0030.00517.68
8.1.40.0090.00017.78
8.1.30.0000.00817.84
8.1.20.0040.00417.84
8.1.10.0000.00817.77
8.1.00.0000.00817.68
8.0.300.0040.00418.77
8.0.290.0000.00717.13
8.0.280.0050.00318.55
8.0.270.0000.00817.44
8.0.260.0000.00717.53
8.0.250.0030.00517.16
8.0.240.0070.00017.21
8.0.230.0000.00717.18
8.0.220.0070.00017.22
8.0.210.0000.00717.17
8.0.200.0030.00317.25
8.0.190.0030.00317.30
8.0.180.0040.00417.23
8.0.170.0020.00517.15
8.0.160.0070.00017.14
8.0.150.0050.00317.21
8.0.140.0040.00417.19
8.0.130.0030.00313.64
8.0.120.0000.00717.17
8.0.110.0080.00017.12
8.0.100.0000.00716.98
8.0.90.0080.00017.24
8.0.80.0070.01417.25
8.0.70.0030.00516.96
8.0.60.0040.00417.03
8.0.50.0000.00817.09
8.0.30.0120.00717.42
8.0.20.0130.00617.61
8.0.10.0050.00317.17
8.0.00.0120.00717.09
7.4.330.0000.00515.23
7.4.320.0030.00316.90
7.4.300.0030.00316.85
7.4.290.0070.00016.80
7.4.280.0070.00316.72
7.4.270.0070.00016.89
7.4.260.0030.00316.78
7.4.250.0000.00716.65
7.4.240.0030.00416.79
7.4.230.0000.00816.95
7.4.220.0140.00416.96
7.4.210.0080.00716.96
7.4.200.0040.00416.89
7.4.160.0100.00616.99
7.4.150.0110.00717.40
7.4.140.0070.01017.86
7.4.130.0120.00816.81
7.4.120.0120.00516.70
7.4.110.0140.00316.64
7.4.100.0100.00716.69
7.4.90.0100.00716.72
7.4.80.0150.00919.39
7.4.70.0090.01216.49
7.4.60.0090.00616.69
7.4.50.0000.00816.68
7.4.40.0110.00516.79
7.4.30.0160.00416.90
7.4.00.0060.00914.93
7.3.330.0000.00613.51
7.3.320.0000.00513.48
7.3.310.0030.00316.60
7.3.300.0070.00016.70
7.3.290.0130.00316.63
7.3.280.0080.00916.56
7.3.270.0090.00917.40
7.3.260.0110.01216.61
7.3.250.0110.00716.69
7.3.240.0100.01016.57
7.3.230.0100.00716.89
7.3.210.0000.01816.77
7.3.200.0070.01419.39
7.3.190.0100.00616.57
7.3.180.0030.01616.85
7.3.170.0100.00716.91
7.3.160.0070.01016.82
7.3.120.0160.00014.92
7.3.10.0060.00916.58
7.3.00.0060.00916.19
7.2.330.0030.01417.05
7.2.320.0070.01117.21
7.2.310.0170.00017.16
7.2.300.0030.01317.18
7.2.290.0090.01716.72
7.2.130.0100.00317.00
7.2.120.0190.00717.04
7.2.110.0140.00316.45
7.2.100.0070.01116.30
7.2.90.0180.00016.57
7.2.80.0060.00916.57
7.2.70.0070.00716.56
7.2.60.0150.00316.74
7.2.50.0220.00016.58
7.2.40.0130.01016.67
7.2.30.0290.00416.71
7.2.20.0200.01117.13
7.2.10.0150.01317.50
7.2.00.0100.01118.12
7.1.250.0130.00815.68
7.1.200.0050.00515.84
7.1.140.0150.00617.07
7.1.130.0120.01516.99
7.1.120.0120.01217.33
7.1.110.0980.01416.65
7.1.100.0110.00617.31
7.1.90.0210.01216.38
7.1.80.0230.00616.15
7.1.70.0170.01116.30
7.1.60.0200.01125.43
7.1.50.0280.00925.19
7.1.40.0230.00424.67
7.1.30.0340.01024.71
7.1.20.0100.01524.79
7.1.10.0430.00615.87
7.1.00.0100.01216.02
7.0.270.0180.00616.77
7.0.260.0150.01116.61
7.0.250.0150.00916.20
7.0.240.0180.00715.99
7.0.230.0270.00716.06
7.0.220.0210.01516.27
7.0.210.0300.00914.99
7.0.200.0100.01216.04
7.0.190.0100.01115.74
7.0.180.0110.00515.61
7.0.170.0100.01515.42
7.0.160.0130.00415.49
7.0.150.0100.00815.47
7.0.140.0100.00915.57
7.0.130.0120.00715.88
7.0.120.0160.00215.76
7.0.110.0070.00915.80
7.0.100.0090.00815.53
7.0.90.0030.01315.49
7.0.80.0330.01115.56
7.0.70.0060.01315.69
7.0.60.0120.00815.49
7.0.50.0090.01515.65
7.0.40.0070.00814.99
7.0.30.0110.00715.01
7.0.20.0050.01614.98
7.0.10.0090.01014.93
7.0.00.0060.01215.02
5.6.300.0100.06920.89
5.6.290.0090.06220.79
5.6.280.0100.05520.97
5.6.270.0150.05120.85
5.6.260.0130.04821.04
5.6.250.0140.04820.95
5.6.240.0170.06420.86
5.6.230.0090.07120.99
5.6.220.0140.06520.98
5.6.210.0100.06021.07
5.6.200.0120.05720.98
5.6.190.0050.07521.04
5.6.180.0170.07720.90
5.6.170.0090.05620.97
5.6.160.0130.07021.06
5.6.150.0150.05321.07
5.6.140.0360.05620.90
5.6.130.0130.05920.98
5.6.120.0120.06120.96
5.6.110.0070.05320.84
5.6.100.0050.05320.95
5.6.90.0050.05420.99
5.6.80.0060.05020.36
5.6.70.0100.04820.23
5.6.60.0080.05220.22
5.6.50.0060.04920.34
5.6.40.0080.06120.21
5.6.30.0120.05520.21
5.6.20.0140.04720.23
5.6.10.0090.05920.34
5.6.00.0120.05220.21
5.5.380.0160.04817.66
5.5.370.0150.06017.48
5.5.360.0150.07517.61
5.5.350.0120.04817.67
5.5.340.0170.04917.99
5.5.330.0140.05818.09
5.5.320.0110.07118.09
5.5.310.0050.06018.01
5.5.300.0100.06217.97
5.5.290.0110.07618.14
5.5.280.0120.06118.09
5.5.270.0110.05118.11
5.5.260.0060.05818.11
5.5.250.0090.04617.82
5.5.240.0260.03817.36
5.5.230.0070.05017.59
5.5.220.0170.04417.52
5.5.210.0070.05117.39
5.5.200.0150.05817.43
5.5.190.0060.05317.49
5.5.180.0100.04617.44
5.5.160.0170.04617.47
5.5.150.0190.04617.52
5.5.140.0120.05317.33
5.5.130.0090.05917.47
5.5.120.0100.05317.48
5.5.110.0120.04617.31
5.5.100.0070.04617.34
5.5.90.0140.05317.27
5.5.80.0060.05017.37
5.5.70.0130.06117.38
5.5.60.0140.03817.29
5.5.50.0090.05017.26
5.5.40.0150.05317.26
5.5.30.0140.05217.28
5.5.20.0110.05217.38
5.5.10.0050.06417.26
5.5.00.0080.05317.22
5.4.450.0160.05819.33
5.4.440.0090.06119.50
5.4.430.0130.04919.44
5.4.420.0050.05019.46
5.4.410.0060.04819.28
5.4.400.0050.04919.11
5.4.390.0030.06518.97
5.4.380.0090.05119.16
5.4.370.0120.06119.05
5.4.360.0060.06319.05
5.4.350.0240.04119.06
5.4.340.0090.05419.13
5.4.320.0090.05119.08
5.4.310.0080.05519.10
5.4.300.0080.05119.08
5.4.290.0080.05319.20
5.4.280.0170.04919.21
5.4.270.0210.04619.09
5.4.260.0060.04919.03
5.4.250.0110.04719.22
5.4.240.0080.05319.30
5.4.230.0170.04919.14
5.4.220.0080.04719.17
5.4.210.0150.06119.00
5.4.200.0180.04819.14
5.4.190.0100.05819.13
5.4.180.0030.05819.03
5.4.170.0100.05019.06
5.4.160.0110.06118.96
5.4.150.0100.07518.96
5.4.140.0040.05816.66
5.4.130.0080.05316.77
5.4.120.0220.05116.69
5.4.110.0050.05116.80
5.4.100.0100.04116.77
5.4.90.0110.05016.73
5.4.80.0090.04516.78
5.4.70.0120.04916.75
5.4.60.0100.06016.78
5.4.50.0120.06216.76
5.4.40.0080.04916.77
5.4.30.0080.06716.69
5.4.20.0100.05816.81
5.4.10.0140.05616.55
5.4.00.0140.04016.22
5.3.290.0070.06015.06
5.3.280.0050.05614.96
5.3.270.0220.05015.09
5.3.260.0110.05815.07
5.3.250.0100.05215.07
5.3.240.0030.06315.03
5.3.230.0080.05115.02
5.3.220.0100.04314.89
5.3.210.0100.05815.03
5.3.200.0150.04614.88
5.3.190.0150.04215.05
5.3.180.0120.04715.04
5.3.170.0100.04315.05
5.3.160.0080.07014.88
5.3.150.0070.05815.06
5.3.140.0150.05714.94
5.3.130.0100.05214.88
5.3.120.0130.06014.88
5.3.110.0140.05615.04
5.3.100.0150.05614.79
5.3.90.0020.05814.73
5.3.80.0060.05114.61
5.3.70.0100.05314.79
5.3.60.0090.05614.66
5.3.50.0110.04814.71
5.3.40.0120.04314.76
5.3.30.0060.05014.68
5.3.20.0090.04814.60
5.3.10.0100.05214.56
5.3.00.0130.04114.55
5.2.170.0100.04513.27
5.2.160.0080.04213.11
5.2.150.0090.03813.31
5.2.140.0050.04113.19
5.2.130.0050.04413.28
5.2.120.0060.03913.25
5.2.110.0110.03713.17
5.2.100.0070.03713.16
5.2.90.0080.04713.26
5.2.80.0080.03913.23
5.2.70.0060.03513.24
5.2.60.0060.04513.26
5.2.50.0050.04713.04
5.2.40.0100.04113.19
5.2.30.0100.04313.22
5.2.20.0080.04313.18
5.2.10.0080.03113.12
5.2.00.0050.03512.89
5.1.60.0080.03512.68
5.1.50.0080.02812.73
5.1.40.0100.03312.60
5.1.30.0100.03812.76
5.1.20.0100.04112.88
5.1.10.0050.03512.77
5.1.00.0070.03812.77
5.0.50.0080.02912.26
5.0.40.0060.02612.26
5.0.30.0080.03612.26
5.0.20.0080.02512.26
5.0.10.0050.02612.26
5.0.00.0030.03912.26
4.4.90.0080.01812.26
4.4.80.0030.02612.26
4.4.70.0050.01612.26
4.4.60.0030.02512.26
4.4.50.0040.02812.26
4.4.40.0050.02912.26
4.4.30.0020.02112.26
4.4.20.0040.03212.26
4.4.10.0060.01712.26
4.4.00.0030.03012.26
4.3.110.0070.01512.26
4.3.100.0050.01912.26
4.3.90.0130.01712.26
4.3.80.0020.03212.26
4.3.70.0020.02412.26
4.3.60.0050.02212.26
4.3.50.0020.03112.26
4.3.40.0080.04112.26
4.3.30.0050.03112.26
4.3.20.0040.02712.26
4.3.10.0020.02112.26
4.3.00.0020.01812.26

preferences:
52.38 ms | 401 KiB | 5 Q