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(); if (version_compare(PHP_VERSION, '5.4.0') < 0) { $dom->loadHTML($html); } else { $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); } $instance = new static(); $toRemove = $instance->walk($dom, $maxLen); // remove any nodes that exceed limit foreach ($toRemove as $child) { $child->parentNode->removeChild($child); } // remove wrapper tags added by DD (doctype, html...) if (version_compare(PHP_VERSION, '5.4.0') < 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(); } 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>Lorem ipsum dolor sit amet <a href="very-long-long-prelong-href-that-should-not-be-cut">Quite long link</a> text after link</p>', 35);

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.0120.00618.93
8.3.50.0160.00316.86
8.3.40.0000.01519.05
8.3.30.0040.01418.91
8.3.20.0050.00222.13
8.3.10.0040.00423.88
8.3.00.0040.00419.94
8.2.180.0080.00816.63
8.2.170.0090.00622.96
8.2.160.0140.00020.64
8.2.150.0090.00024.18
8.2.140.0090.00024.66
8.2.130.0050.00326.16
8.2.120.0110.00420.71
8.2.110.0060.00321.23
8.2.100.0070.00418.03
8.2.90.0040.00420.69
8.2.80.0030.00518.30
8.2.70.0030.00618.15
8.2.60.0100.00418.02
8.2.50.0060.00817.79
8.2.40.0080.00417.99
8.2.30.0070.00517.89
8.2.20.0080.00518.11
8.2.10.0120.00017.60
8.2.00.0070.00417.95
8.1.280.0110.00425.92
8.1.270.0030.00722.25
8.1.260.0030.00526.35
8.1.250.0080.00028.09
8.1.240.0060.00319.47
8.1.230.0090.00322.07
8.1.220.0030.00518.05
8.1.210.0040.00419.20
8.1.200.0030.00617.48
8.1.190.0130.00317.36
8.1.180.0100.00317.85
8.1.170.0080.00418.10
8.1.160.0110.00118.19
8.1.150.0080.00519.02
8.1.140.0080.00417.74
8.1.130.0030.00918.33
8.1.120.0040.00817.64
8.1.110.0090.00417.44
8.1.100.0110.00117.61
8.1.90.0060.00617.49
8.1.80.0100.00217.48
8.1.70.0090.00417.44
8.1.60.0080.00617.74
8.1.50.0090.00517.63
8.1.40.0070.00617.64
8.1.30.0090.00417.72
8.1.20.0050.00817.74
8.1.10.0090.00417.66
8.1.00.0060.00717.55
8.0.300.0000.00820.11
8.0.290.0030.00616.88
8.0.280.0050.00717.85
8.0.270.0040.00817.17
8.0.260.0070.00517.00
8.0.250.0130.00016.98
8.0.240.0060.00717.12
8.0.230.0060.00617.04
8.0.220.0110.00117.04
8.0.210.0120.00016.80
8.0.200.0080.00616.89
8.0.190.0060.00616.97
8.0.180.0110.00316.99
8.0.170.0060.00617.00
8.0.160.0090.00417.01
8.0.150.0060.00616.95
8.0.140.0080.00417.05
8.0.130.0090.00215.24
8.0.120.0060.00516.88
8.0.110.0080.00416.90
8.0.100.0060.00617.06
8.0.90.0080.00417.09
8.0.80.0050.01116.90
8.0.70.0080.00417.12
8.0.60.0080.00416.98
8.0.50.0070.00716.91
8.0.30.0110.01117.22
8.0.20.0090.00817.27
8.0.10.0080.00817.11
8.0.00.0100.00917.10
7.4.330.0030.00616.03
7.4.320.0050.00616.71
7.4.300.0040.00716.70
7.4.290.0120.00016.64
7.4.280.0040.00816.65
7.4.270.0090.00416.63
7.4.260.0080.00516.73
7.4.250.0030.00816.58
7.4.240.0060.00616.67
7.4.230.0100.00216.68
7.4.220.0090.00316.80
7.4.210.0070.00816.65
7.4.200.0080.00416.75
7.4.190.0120.00416.63
7.4.180.0100.00516.59
7.4.160.0100.00616.81
7.4.150.0080.00816.62
7.4.140.0110.00817.39
7.4.130.0090.00716.71
7.4.120.0090.00816.75
7.4.110.0090.01016.75
7.4.100.0100.00816.64
7.4.90.0080.00816.63
7.4.80.0090.00717.23
7.4.70.0120.00716.62
7.4.60.0090.00816.69
7.4.50.0100.00816.71
7.4.40.0090.00916.61
7.4.30.0090.00816.64
7.4.20.0120.00716.66
7.4.10.0100.00816.66
7.4.00.0070.01216.27
7.3.330.0100.00115.05
7.3.320.0050.00515.06
7.3.310.0050.00516.57
7.3.300.0030.00716.59
7.3.290.0080.00416.61
7.3.280.0090.00816.54
7.3.270.0120.00716.52
7.3.260.0090.01016.67
7.3.250.0070.00816.54
7.3.240.0050.01216.65
7.3.230.0110.00716.61
7.3.220.0110.00616.53
7.3.210.0120.00416.57
7.3.200.0100.00616.61
7.3.190.0090.00816.64
7.3.180.0100.00816.58
7.3.170.0100.01016.60
7.3.160.0090.00816.62
7.3.150.0090.00916.64
7.3.140.0090.00816.68
7.3.130.0090.00916.64
7.3.120.0130.01116.17
7.3.110.0100.00716.11
7.3.100.0090.00916.02
7.3.90.0070.01116.16
7.3.80.0090.01016.07
7.3.70.0110.01016.11
7.3.60.0080.01015.94
7.3.50.0110.00815.89
7.3.40.0100.00815.90
7.3.30.0110.01015.81
7.3.20.0940.00816.20
7.3.10.0690.01116.08
7.3.00.0590.00716.11
7.2.340.0140.01316.63
7.2.330.0140.00916.70
7.2.320.0160.00716.74
7.2.310.0120.00916.72
7.2.300.0130.00916.73
7.2.290.0100.01116.80
7.2.280.0150.00816.74
7.2.270.0110.01016.74
7.2.260.0110.01016.87
7.2.250.0120.01016.57
7.2.240.0120.00916.21
7.2.230.0090.01116.21
7.2.220.0120.00916.21
7.2.210.0120.00916.26
7.2.200.0120.00916.30
7.2.190.0120.01016.08
7.2.180.0110.01016.12
7.2.170.0100.01016.07
7.2.160.0130.00816.08
7.2.150.0900.01016.34
7.2.140.0850.01116.39
7.2.130.0510.01016.37
7.2.120.0400.01016.40
7.2.110.0480.00816.38
7.2.100.0540.00816.37
7.2.90.0370.00716.27
7.2.80.0460.00816.30
7.2.70.0370.01016.32
7.2.60.0400.01016.24
7.2.50.0410.00816.23
7.2.40.0470.00916.38
7.2.30.0460.00816.44
7.2.20.0250.00916.38
7.2.10.0400.01016.31
7.2.00.0410.01016.40
7.1.330.0140.00915.38
7.1.320.0120.00915.46
7.1.310.0110.00815.49
7.1.300.0140.00715.26
7.1.290.0130.00815.28
7.1.280.0120.00815.29
7.1.270.0820.00715.28
7.1.260.0690.00815.20
7.1.250.0550.00915.17
7.1.240.0430.00715.19
7.1.230.0470.00715.23
7.1.220.0370.01015.24
7.1.210.0240.00715.11
7.1.200.0500.00915.20
7.1.190.0420.00915.17
7.1.180.0350.00815.13
7.1.170.0410.00815.06
7.1.160.0490.00915.35
7.1.150.0400.01015.31
7.1.140.0580.00915.35
7.1.130.0390.00815.36
7.1.120.0500.00915.31
7.1.110.0370.00915.33
7.1.100.0390.01015.31
7.1.90.0330.00815.30
7.1.80.0320.00815.34
7.1.70.0420.01015.11
7.1.60.0420.00818.38
7.1.50.0440.01018.34
7.1.40.0390.00818.25
7.1.30.0370.00918.23
7.1.20.0390.00918.30
7.1.10.0330.00915.12
7.1.00.0400.01015.06
7.0.330.0630.00914.83
7.0.320.0110.00915.33
7.0.310.0140.00715.37
7.0.300.0150.00715.38
7.0.290.0140.00815.37
7.0.280.0140.00715.37
7.0.270.0180.00615.40
7.0.260.0140.00715.41
7.0.250.0160.00715.46
7.0.240.0150.00815.49
7.0.230.0150.00815.39
7.0.220.0180.00715.41
7.0.210.0160.00715.40
7.0.200.0150.00715.40
7.0.190.0150.00815.44
7.0.180.0170.01015.49
7.0.170.0150.00815.49
7.0.160.0150.00915.40
7.0.150.0190.00515.37
7.0.140.0190.00715.34
7.0.130.0130.00815.41
7.0.120.0140.00615.38
7.0.110.0170.00615.41
7.0.100.0150.00815.40
7.0.90.0150.00815.38
7.0.80.0160.01115.43
7.0.70.0150.00815.36
7.0.60.0160.01115.40
7.0.50.0150.00915.42
7.0.40.0140.00815.46
7.0.30.0140.00815.43
7.0.20.0130.00915.43
7.0.10.0150.00815.44
7.0.00.0130.01115.35
5.6.400.0130.01015.58
5.6.390.0140.01015.48
5.6.380.0130.01015.40
5.6.370.0140.00816.10
5.6.360.0120.01116.09
5.6.350.0140.01016.04
5.6.340.0130.01016.11
5.6.330.0140.00916.02
5.6.320.0170.00616.11
5.6.310.0140.00916.08
5.6.300.0140.01115.95
5.6.290.0170.00816.12
5.6.280.0140.00916.10
5.6.270.0130.01016.08
5.6.260.0140.00916.01
5.6.250.0150.00816.08
5.6.240.0170.00916.07
5.6.230.0130.01016.09
5.6.220.0130.01116.09
5.6.210.0150.00915.98
5.6.200.0150.01116.11
5.6.190.0150.00916.16
5.6.180.0110.01116.03
5.6.170.0130.01116.18
5.6.160.0150.01116.05
5.6.150.0140.00816.11
5.6.140.0130.01116.07
5.6.130.0170.00816.07
5.6.120.0160.00916.07
5.6.110.0120.01116.08
5.6.100.0160.01016.12
5.6.90.0150.01016.14
5.6.80.0130.01016.07
5.6.70.0120.01016.11
5.6.60.0130.01016.13
5.6.50.0120.00916.00
5.6.40.0100.01116.08
5.6.30.0110.00916.04
5.6.20.0110.01215.98
5.6.10.0130.01015.98
5.6.00.0150.01116.05

preferences:
60.86 ms | 401 KiB | 5 Q