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

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.0070.00718.52
8.3.50.0060.01122.17
8.3.40.0090.00619.05
8.3.30.0160.00319.10
8.3.20.0080.00020.59
8.3.10.0060.00323.73
8.3.00.0040.00420.06
8.2.180.0190.00318.54
8.2.170.0130.00622.96
8.2.160.0170.00020.47
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0200.00026.16
8.2.120.0110.00721.20
8.2.110.0050.00522.26
8.2.100.0090.00317.97
8.2.90.0000.00819.41
8.2.80.0030.00517.97
8.2.70.0060.00317.75
8.2.60.0000.00818.30
8.2.50.0050.00518.07
8.2.40.0040.00419.71
8.2.30.0040.00418.57
8.2.20.0070.00017.89
8.2.10.0040.00418.41
8.2.00.0040.00418.08
8.1.280.0150.00025.92
8.1.270.0090.00024.27
8.1.260.0070.00026.35
8.1.250.0000.00828.09
8.1.240.0060.00324.07
8.1.230.0040.00819.32
8.1.220.0040.00417.91
8.1.210.0000.00818.77
8.1.200.0080.00417.73
8.1.190.0090.00017.48
8.1.180.0000.00918.10
8.1.170.0060.00318.75
8.1.160.0000.00722.24
8.1.150.0040.00418.89
8.1.140.0040.00417.82
8.1.130.0030.00518.26
8.1.120.0000.00717.70
8.1.110.0040.00417.74
8.1.100.0040.00417.68
8.1.90.0000.00717.71
8.1.80.0000.00717.66
8.1.70.0000.00717.77
8.1.60.0060.00317.86
8.1.50.0000.00917.70
8.1.40.0040.00417.72
8.1.30.0060.00317.97
8.1.20.0040.00417.93
8.1.10.0000.00817.78
8.1.00.0080.00017.70
8.0.300.0000.00718.77
8.0.290.0000.00717.25
8.0.280.0030.00518.76
8.0.270.0030.00317.44
8.0.260.0070.00017.56
8.0.250.0030.00317.22
8.0.240.0040.00417.25
8.0.230.0070.00017.18
8.0.220.0030.00317.20
8.0.210.0020.00517.20
8.0.200.0000.00617.18
8.0.190.0050.00317.22
8.0.180.0040.00417.21
8.0.170.0040.00417.15
8.0.160.0070.00017.17
8.0.150.0040.00417.17
8.0.140.0030.00617.17
8.0.130.0000.00613.77
8.0.120.0040.00417.15
8.0.110.0040.00417.28
8.0.100.0000.00717.07
8.0.90.0040.00417.26
8.0.80.0030.01317.24
8.0.70.0080.00017.28
8.0.60.0040.00417.34
8.0.50.0030.00617.01
8.0.30.0110.00817.46
8.0.20.0060.01317.46
8.0.10.0030.00517.21
8.0.00.0100.00817.04
7.4.330.0030.00315.27
7.4.320.0000.00616.85
7.4.300.0000.00616.64
7.4.290.0000.00716.82
7.4.280.0030.00316.77
7.4.270.0000.00716.74
7.4.260.0050.00516.86
7.4.250.0040.00416.91
7.4.240.0020.00516.71
7.4.230.0030.00316.61
7.4.220.0070.01716.73
7.4.210.0100.00316.93
7.4.200.0050.00216.55
7.4.160.0110.00616.84
7.4.150.0120.00917.40
7.4.140.0100.00817.86
7.4.130.0080.01116.89
7.4.120.0090.00816.84
7.4.110.0120.00616.93
7.4.100.0130.00316.88
7.4.90.0060.01016.86
7.4.80.0120.00619.39
7.4.70.0070.01016.84
7.4.60.0060.00916.77
7.4.50.0080.00016.63
7.4.40.0070.01016.75
7.4.30.0060.01016.71
7.4.00.0060.01115.00
7.3.330.0000.00613.56
7.3.320.0060.00013.52
7.3.310.0000.00716.51
7.3.300.0050.00216.70
7.3.290.0110.00316.50
7.3.280.0110.00816.60
7.3.270.0090.01217.40
7.3.260.0030.01516.67
7.3.250.0130.00416.70
7.3.240.0090.00916.59
7.3.230.0040.01416.79
7.3.210.0100.00716.65
7.3.200.0030.01319.39
7.3.190.0080.01216.71
7.3.180.0030.01416.59
7.3.170.0040.01516.70
7.3.160.0090.00616.96
7.3.120.0080.00714.89
7.3.110.0080.01114.89
7.3.100.0070.01014.84
7.3.90.0030.01114.85
7.3.80.0080.00615.12
7.3.70.0060.00514.92
7.3.60.0010.01314.94
7.3.50.0080.00715.03
7.3.40.0060.00814.84
7.3.30.0070.00714.73
7.3.20.0070.01016.79
7.3.10.0090.00716.66
7.3.00.0090.00716.64
7.2.330.0090.00917.04
7.2.320.0090.00916.77
7.2.310.0060.01217.04
7.2.300.0120.00417.04
7.2.290.0070.01017.13
7.2.250.0120.00515.29
7.2.240.0090.01014.99
7.2.230.0050.01015.06
7.2.220.0020.01115.27
7.2.210.0070.00815.20
7.2.200.0020.01315.20
7.2.190.0090.00915.15
7.2.180.0050.01015.36
7.2.170.0090.00815.27
7.2.160.0080.00315.18
7.2.150.0030.01216.99
7.2.140.0040.00916.72
7.2.130.0070.00716.97
7.2.120.0100.00917.10
7.2.110.0100.00516.83
7.2.100.0060.00917.01
7.2.90.0080.00617.19
7.2.80.0080.00617.11
7.2.70.0100.00716.93
7.2.60.0080.00817.05
7.2.50.0020.01016.82
7.2.40.0110.00617.04
7.2.30.0110.00517.03
7.2.20.0070.00916.92
7.2.10.0080.01017.00
7.2.00.0080.00717.62
7.1.330.0040.01415.77
7.1.320.0070.01016.04
7.1.310.0060.00615.94
7.1.300.0050.01115.85
7.1.290.0080.00515.75
7.1.280.0030.01415.78
7.1.270.0090.00515.97
7.1.260.0100.00715.93
7.1.250.0140.00515.99
7.1.240.0060.00715.94
7.1.230.0090.00715.91
7.1.220.0030.01315.87
7.1.210.0040.00616.06
7.1.200.0060.00816.02
7.1.190.0120.00616.02
7.1.180.0060.00615.72
7.1.170.0080.00615.86
7.1.160.0030.01015.87
7.1.150.0090.00515.87
7.1.140.0080.00616.06
7.1.130.0050.00815.99
7.1.120.0070.00615.91
7.1.110.0090.00515.62
7.1.100.0000.01216.68
7.1.90.0060.00616.03
7.1.80.0050.00615.95
7.1.70.0060.00416.29
7.1.60.0050.01017.06
7.1.50.0040.01416.23
7.1.40.0090.00515.93
7.1.30.0070.01015.74
7.1.20.0030.01215.88
7.1.10.0060.00715.73
7.1.00.0030.03218.21
7.0.330.0020.01015.57
7.0.320.0060.00915.47
7.0.310.0070.00915.44
7.0.300.0070.00515.31
7.0.290.0100.00515.50
7.0.280.0070.00715.56
7.0.270.0100.00415.55
7.0.260.0030.00815.45
7.0.250.0040.00915.56
7.0.240.0050.00515.46
7.0.230.0030.01015.50
7.0.220.0070.00715.64
7.0.210.0050.01015.31
7.0.200.0080.00616.04
7.0.190.0090.00415.35
7.0.180.0080.00815.54
7.0.170.0050.01015.43
7.0.160.0100.00515.59
7.0.150.0080.00615.62
7.0.140.0050.03017.69
7.0.130.0040.00715.46
7.0.120.0060.00715.40
7.0.110.0020.01515.58
7.0.100.0060.03216.84
7.0.90.0120.02216.94
7.0.80.0080.01817.01
7.0.70.0050.03317.01
7.0.60.0170.02317.00
7.0.50.0080.02917.09
7.0.40.0060.03415.74
7.0.30.0090.02815.82
7.0.20.0060.03815.73
7.0.10.0050.02315.69
7.0.00.0090.02215.79
5.6.400.0020.01214.67
5.6.390.0020.01214.46
5.6.380.0090.00514.59
5.6.370.0030.01214.67
5.6.360.0040.00914.71
5.6.350.0040.01114.64
5.6.340.0080.00514.80
5.6.330.0120.00214.81
5.6.320.0040.00714.37
5.6.310.0020.01114.73
5.6.300.0020.01214.52
5.6.290.0040.01014.61
5.6.280.0050.02916.87
5.6.270.0070.00714.79
5.6.260.0090.00414.88
5.6.250.0030.03716.72
5.6.240.0100.03116.58
5.6.230.0040.02116.67
5.6.220.0100.02916.66
5.6.210.0080.02016.85
5.6.200.0040.02516.86
5.6.190.0050.03716.70
5.6.180.0100.02416.85
5.6.170.0070.02416.64
5.6.160.0100.02216.75
5.6.150.0120.01916.63
5.6.140.0100.02216.73
5.6.130.0130.03016.82
5.6.120.0060.03416.77
5.6.110.0060.03216.76
5.6.100.0090.02916.67
5.6.90.0110.02716.79
5.6.80.0060.02216.39
5.6.70.0060.02416.58
5.6.60.0070.02516.50
5.6.50.0040.03316.41
5.6.40.0060.02116.39
5.6.30.0030.03416.50
5.6.20.0040.03416.54
5.6.10.0050.02916.43
5.6.00.0090.02916.48
5.5.380.0070.04020.42
5.5.370.0000.06020.43
5.5.360.0130.06020.45
5.5.350.0130.08020.49
5.5.340.0070.08020.81
5.5.330.0070.08020.74
5.5.320.0070.05320.95
5.5.310.0130.08020.78
5.5.300.0070.08020.90
5.5.290.0130.08020.74
5.5.280.0000.08720.81
5.5.270.0070.04720.92
5.5.260.0070.05720.91
5.5.250.0100.04720.70
5.5.240.0070.06320.13
5.5.230.0030.09020.27
5.5.220.0200.07320.30
5.5.210.0030.05020.27
5.5.200.0100.08020.20
5.5.190.0170.03720.00
5.5.180.0000.05020.12
5.5.160.0100.08020.12
5.5.150.0130.07720.22
5.5.140.0100.05020.27
5.5.130.0100.07720.16
5.5.120.0030.08020.09
5.5.110.0130.06020.23
5.5.100.0100.08020.14
5.5.90.0100.07720.10
5.5.80.0030.08720.05
5.5.70.0070.07720.07
5.5.60.0000.08020.06
5.5.50.0070.07320.09
5.5.40.0070.06720.07
5.5.30.0070.08020.13
5.5.20.0030.07720.16
5.5.10.0170.06719.96
5.5.00.0130.03020.04
5.4.450.0100.08319.58
5.4.440.0030.06319.34
5.4.430.0070.07719.51
5.4.420.0030.08319.60
5.4.410.0030.07719.12
5.4.400.0030.07319.25
5.4.390.0130.06319.28
5.4.380.0030.06019.21
5.4.370.0100.07718.97
5.4.360.0170.03319.21
5.4.350.0000.08019.19
5.4.340.0000.05019.03
5.4.320.0270.06019.22
5.4.310.0070.05019.18
5.4.300.0030.05719.18
5.4.290.0100.07718.96
5.4.280.0170.04019.30
5.4.270.0070.06018.93
5.4.260.0070.04719.18
5.4.250.0030.04719.21
5.4.240.0170.06019.27
5.4.230.0070.08018.94
5.4.220.0000.08719.30
5.4.210.0070.08019.27
5.4.200.0100.03718.96
5.4.190.0130.03319.16
5.4.180.0030.04019.20
5.4.170.0100.07319.15
5.4.160.0030.07319.07
5.4.150.0000.07019.14
5.4.140.0070.07016.49
5.4.130.0000.08016.46
5.4.120.0030.07316.48
5.4.110.0000.08016.68
5.4.100.0070.07716.46
5.4.90.0070.04016.55
5.4.80.0000.03716.53
5.4.70.0130.06316.46
5.4.60.0070.07016.48
5.4.50.0000.03716.54
5.4.40.0030.03316.42
5.4.30.0000.04016.51
5.4.20.0100.05716.44
5.4.10.0070.03016.60
5.4.00.0000.03315.99
5.3.290.0100.04714.80
5.3.280.0100.06014.73
5.3.270.0100.03714.72
5.3.260.0070.07314.74
5.3.250.0070.07314.68
5.3.240.0070.03014.78
5.3.230.0130.06314.66
5.3.220.0130.06314.82
5.3.210.0030.07014.72
5.3.200.0100.06714.75
5.3.190.0000.07714.61
5.3.180.0030.05314.60
5.3.170.0000.05714.72
5.3.160.0000.06014.77
5.3.150.0000.04314.58
5.3.140.0000.05014.71
5.3.130.0130.05314.72
5.3.120.0030.08014.70
5.3.110.0030.03314.70
5.3.100.0030.04314.29
5.3.90.0030.05714.16
5.3.80.0030.03314.27
5.3.70.0000.06314.22
5.3.60.0100.07314.14
5.3.50.0100.07013.93
5.3.40.0000.07314.23
5.3.30.0030.06714.11
5.3.20.0070.02713.77
5.3.10.0070.07713.73
5.3.00.0000.03313.66
5.2.170.0130.05011.07
5.2.160.0030.04311.07
5.2.150.0000.06711.00
5.2.140.0030.02311.00
5.2.130.0030.06011.00
5.2.120.0030.05711.15
5.2.110.0070.05710.93
5.2.100.0030.04011.04
5.2.90.0070.05711.16
5.2.80.0070.03711.12
5.2.70.0030.04011.13
5.2.60.0070.05010.98
5.2.50.0030.05010.88
5.2.40.0030.02310.89
5.2.30.0030.06010.88
5.2.20.0000.04310.86
5.2.10.0030.02310.76
5.2.00.0030.05010.83
5.1.60.0030.04010.09
5.1.50.0000.05710.10
5.1.40.0070.04310.10
5.1.30.0070.03310.46
5.1.20.0000.03310.41
5.1.10.0070.03010.15
5.1.00.0030.05310.06
5.0.50.0000.0408.66
5.0.40.0000.0408.42
5.0.30.0030.0308.32
5.0.20.0000.0338.26
5.0.10.0030.0378.29
5.0.00.0030.0608.24
4.4.90.0000.0377.17
4.4.80.0000.0277.17
4.4.70.0070.0277.17
4.4.60.0030.0337.17
4.4.50.0000.0377.17
4.4.40.0030.0507.17
4.4.30.0000.0137.17
4.4.20.0070.0277.17
4.4.10.0000.0337.17
4.4.00.0000.0577.17
4.3.110.0030.0337.17
4.3.100.0000.0377.17
4.3.90.0000.0277.17
4.3.80.0000.0477.17
4.3.70.0000.0177.17
4.3.60.0030.0307.17
4.3.50.0000.0137.17
4.3.40.0000.0477.17
4.3.30.0030.0207.17
4.3.20.0000.0177.17
4.3.10.0000.0137.17
4.3.00.0000.0337.17

preferences:
37.27 ms | 401 KiB | 5 Q