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 sadsdsdsd trim </strong>', 10);

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.0100.01018.55
8.3.50.0140.00621.33
8.3.40.0120.00918.90
8.3.30.0090.00620.55
8.3.20.0000.00820.61
8.3.10.0040.00421.40
8.3.00.0050.00323.89
8.2.180.0110.00816.88
8.2.170.0150.00622.96
8.2.160.0000.01420.43
8.2.150.0040.00424.18
8.2.140.0000.00824.66
8.2.130.0040.00426.16
8.2.120.0050.00321.22
8.2.110.0100.00022.32
8.2.100.0060.00618.16
8.2.90.0040.00419.60
8.2.80.0030.00619.46
8.2.70.0050.00517.88
8.2.60.0040.00418.30
8.2.50.0070.00318.07
8.2.40.0050.00320.13
8.2.30.0030.00619.59
8.2.20.0040.00418.02
8.2.10.0000.00817.92
8.2.00.0040.00417.91
8.1.280.0110.00825.92
8.1.270.0000.00823.99
8.1.260.0090.00026.35
8.1.250.0040.00428.09
8.1.240.0060.00322.70
8.1.230.0110.00022.91
8.1.220.0060.00318.77
8.1.210.0000.00819.04
8.1.200.0030.00617.47
8.1.190.0030.00617.77
8.1.180.0060.00318.10
8.1.170.0030.00518.78
8.1.160.0030.00519.30
8.1.150.0050.00318.86
8.1.140.0000.00817.63
8.1.130.0040.00418.06
8.1.120.0040.00417.66
8.1.110.0000.00717.67
8.1.100.0000.00817.65
8.1.90.0050.00317.62
8.1.80.0080.00017.66
8.1.70.0040.00417.65
8.1.60.0090.00317.82
8.1.50.0000.00817.81
8.1.40.0040.00417.80
8.1.30.0000.00917.97
8.1.20.0090.00017.93
8.1.10.0030.00617.81
8.1.00.0050.00317.64
8.0.300.0000.00820.23
8.0.290.0060.00317.54
8.0.280.0040.00418.73
8.0.270.0040.00417.47
8.0.260.0040.00417.05
8.0.250.0040.00417.17
8.0.240.0000.00717.20
8.0.230.0040.00417.29
8.0.220.0070.00017.22
8.0.210.0000.00817.23
8.0.200.0000.00617.28
8.0.190.0000.00817.19
8.0.180.0040.00417.25
8.0.170.0070.00017.20
8.0.160.0000.00717.18
8.0.150.0050.00217.16
8.0.140.0090.00017.15
8.0.130.0060.00013.63
8.0.120.0030.00517.20
8.0.110.0050.00317.11
8.0.100.0070.00017.19
8.0.90.0040.00417.01
8.0.80.0030.01317.34
8.0.70.0030.00517.38
8.0.60.0070.00017.01
8.0.50.0000.00717.18
8.0.30.0130.00517.38
8.0.20.0060.01217.40
8.0.10.0040.00417.26
8.0.00.0070.01417.11
7.4.330.0030.00315.16
7.4.320.0000.00716.73
7.4.300.0000.00716.69
7.4.290.0040.00416.74
7.4.280.0000.00716.80
7.4.270.0000.00716.83
7.4.260.0060.00013.50
7.4.250.0030.00516.80
7.4.240.0040.00416.83
7.4.230.0000.00716.54
7.4.220.0090.00916.91
7.4.210.0060.00916.84
7.4.200.0000.00716.88
7.4.190.0040.00416.98
7.4.160.0110.00516.86
7.4.150.0110.00617.40
7.4.140.0130.00617.86
7.4.130.0120.00716.79
7.4.120.0110.00616.91
7.4.110.0040.01416.85
7.4.100.0140.00716.59
7.4.90.0130.01316.87
7.4.80.0060.01319.39
7.4.70.0080.01216.85
7.4.60.0160.00616.74
7.4.50.0040.00417.00
7.4.40.0060.00622.77
7.4.30.0090.01616.86
7.4.00.0040.01215.16
7.3.330.0000.00513.55
7.3.320.0050.00013.40
7.3.310.0070.00016.58
7.3.300.0030.00316.66
7.3.290.0070.00716.70
7.3.280.0080.01116.61
7.3.270.0090.00917.40
7.3.260.0120.00818.24
7.3.250.0150.00316.76
7.3.240.0170.00016.76
7.3.230.0090.00816.72
7.3.210.0090.01216.58
7.3.200.0170.00619.39
7.3.190.0120.00616.70
7.3.180.0070.01416.55
7.3.170.0090.00916.91
7.3.160.0080.00816.70
7.3.120.0000.01615.21
7.2.330.0140.00517.09
7.2.320.0180.00017.02
7.2.310.0110.00716.83
7.2.300.0090.00916.90
7.2.290.0100.00716.94
7.2.60.0070.00416.88
7.2.00.0030.01019.30
7.1.200.0060.00615.67
7.1.100.0000.01218.42
7.1.70.0030.00516.94
7.1.60.0070.01819.17
7.1.50.0280.01234.86
7.1.00.0000.03722.51
7.0.200.0000.00816.90
7.0.140.0070.07022.25
7.0.100.0100.08019.99
7.0.90.0100.06020.13
7.0.80.0100.07020.11
7.0.70.0070.08319.96
7.0.60.0100.08720.02
7.0.50.0170.06320.39
7.0.40.0170.07720.18
7.0.30.0100.08020.14
7.0.20.0030.05720.07
7.0.10.0030.05020.16
7.0.00.0070.05320.14
5.6.280.0100.07021.05
5.6.250.0030.06720.81
5.6.240.0070.09020.62
5.6.230.0030.08720.73
5.6.220.0100.07720.67
5.6.210.0130.04020.81
5.6.200.0070.09021.10
5.6.190.0100.08021.14
5.6.180.0130.07721.18
5.6.170.0030.06021.11
5.6.160.0130.07721.02
5.6.150.0030.04321.14
5.6.140.0070.04321.09
5.6.130.0100.08321.23
5.6.120.0200.07021.19
5.6.110.0170.06721.14
5.6.100.0000.05721.05
5.6.90.0030.07721.09
5.6.80.0070.07020.59
5.6.70.0000.08020.49
5.6.60.0170.07020.44
5.6.50.0130.06320.38
5.6.40.0000.08320.52
5.6.30.0000.05020.37
5.6.20.0130.06720.45
5.6.10.0130.06320.50
5.6.00.0100.04720.52
5.5.380.0070.07020.57
5.5.370.0100.07720.46
5.5.360.0130.07020.42
5.5.350.0100.08320.50
5.5.340.0030.07720.95
5.5.330.0130.07720.93
5.5.320.0070.08320.86
5.5.310.0130.07320.95
5.5.300.0100.06320.70
5.5.290.0070.05320.96
5.5.280.0170.07721.00
5.5.270.0070.08020.82
5.5.260.0070.04020.94
5.5.250.0030.05720.77
5.5.240.0170.06720.35
5.5.230.0070.09020.27
5.5.220.0000.06720.33
5.5.210.0100.08020.28
5.5.200.0100.07020.33
5.5.190.0130.07720.26
5.5.180.0100.03720.32
5.5.160.0070.08020.18
5.5.150.0030.08320.20
5.5.140.0100.07720.30
5.5.130.0100.07320.35
5.5.120.0070.04720.33
5.5.110.0070.05020.30
5.5.100.0100.08020.14
5.5.90.0100.04020.22
5.5.80.0130.07320.20
5.5.70.0070.04320.19
5.5.60.0200.04020.00
5.5.50.0070.07720.21
5.5.40.0170.06720.11
5.5.30.0030.07720.11
5.5.20.0170.07020.17
5.5.10.0100.07720.09
5.5.00.0230.06020.12
5.4.450.0030.07319.56
5.4.440.0000.08719.51
5.4.430.0170.07019.26
5.4.420.0000.08319.43
5.4.410.0030.06019.42
5.4.400.0070.07319.02
5.4.390.0100.07718.92
5.4.380.0070.07719.11
5.4.370.0070.08019.20
5.4.360.0000.10019.21
5.4.350.0030.04319.30
5.4.340.0070.07719.16
5.4.320.0030.08719.12
5.4.310.0000.04719.22
5.4.300.0030.07019.10
5.4.290.0130.07019.11
5.4.280.0100.07319.25
5.4.270.0030.04719.20
5.4.260.0070.07719.02
5.4.250.0100.07719.29
5.4.240.0070.07719.15
5.4.230.0100.07319.17
5.4.220.0100.07319.27
5.4.210.0100.07719.01
5.4.200.0030.05018.96
5.4.190.0030.06319.20
5.4.180.0030.07319.26
5.4.170.0030.04019.22
5.4.160.0130.05019.18
5.4.150.0130.06719.25
5.4.140.0130.06316.55
5.4.130.0130.06716.56
5.4.120.0000.06716.54
5.4.110.0070.07716.63
5.4.100.0030.05716.51
5.4.90.0200.06016.47
5.4.80.0070.06716.61
5.4.70.0030.04316.62
5.4.60.0070.04016.49
5.4.50.0070.07716.56
5.4.40.0100.04716.61
5.4.30.0070.07316.46
5.4.20.0030.06716.43
5.4.10.0100.04316.43
5.4.00.0000.06316.00
5.3.290.0100.07014.83
5.3.280.0130.07014.74
5.3.270.0130.07014.81
5.3.260.0070.07714.75
5.3.250.0100.07014.81
5.3.240.0000.08314.62
5.3.230.0100.07014.75
5.3.220.0000.04714.63
5.3.210.0100.07014.72
5.3.200.0170.07014.71
5.3.190.0030.08314.82
5.3.180.0030.04714.72
5.3.170.0130.06714.71
5.3.160.0030.05314.72
5.3.150.0100.07014.58
5.3.140.0130.06314.79
5.3.130.0030.08714.73
5.3.120.0070.07314.71
5.3.110.0130.05314.78
5.3.100.0070.08014.20
5.3.90.0030.08014.09
5.3.80.0070.06714.20
5.3.70.0070.07014.16
5.3.60.0030.07014.01
5.3.50.0030.07714.11
5.3.40.0070.07014.10
5.3.30.0070.04714.00
5.3.20.0030.07313.86
5.3.10.0070.07013.80
5.3.00.0100.06713.68
5.2.170.0000.06711.29
5.2.160.0070.04011.00
5.2.150.0130.05311.20
5.2.140.0070.06311.04
5.2.130.0130.05011.22
5.2.120.0070.03011.02
5.2.110.0070.05711.09
5.2.100.0030.06311.24
5.2.90.0070.06011.09
5.2.80.0070.06311.09
5.2.70.0070.05311.23
5.2.60.0100.05311.11
5.2.50.0070.05311.09
5.2.40.0030.06010.91
5.2.30.0100.05710.88
5.2.20.0070.05310.93
5.2.10.0000.06310.89
5.2.00.0070.05710.86
5.1.60.0030.05310.08
5.1.50.0030.0579.99
5.1.40.0030.0539.98
5.1.30.0030.05710.34
5.1.20.0000.06010.41
5.1.10.0070.05710.08
5.1.00.0070.05710.16
5.0.50.0100.0408.67
5.0.40.0000.0438.47
5.0.30.0030.0638.47
5.0.20.0100.0378.47
5.0.10.0070.0308.47
5.0.00.0000.0678.47
4.4.90.0030.0378.47
4.4.80.0030.0338.47
4.4.70.0000.0378.47
4.4.60.0000.0338.47
4.4.50.0000.0408.47
4.4.40.0000.0578.47
4.4.30.0100.0178.47
4.4.20.0030.0378.47
4.4.10.0030.0208.47
4.4.00.0070.0508.47
4.3.110.0000.0378.47
4.3.100.0000.0238.47
4.3.90.0070.0308.47
4.3.80.0070.0508.47
4.3.70.0030.0138.47
4.3.60.0030.0338.47
4.3.50.0070.0278.47
4.3.40.0100.0408.47
4.3.30.0030.0338.47
4.3.20.0000.0378.47
4.3.10.0000.0308.47
4.3.00.0000.0378.47

preferences:
41.41 ms | 401 KiB | 5 Q