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 sdsdsdsd haz <strong> html sadsdsdsd 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.0190.00318.68
8.3.50.0100.01221.32
8.3.40.0060.01618.94
8.3.30.0070.00720.62
8.3.20.0060.00320.84
8.3.10.0040.00421.30
8.3.00.0060.00323.95
8.2.180.0070.01116.75
8.2.170.0100.00722.96
8.2.160.0070.00720.60
8.2.150.0080.00024.18
8.2.140.0080.00024.66
8.2.130.0050.00326.16
8.2.120.0070.00417.88
8.2.110.0000.00922.53
8.2.100.0090.00317.97
8.2.90.0050.00319.48
8.2.80.0080.00019.22
8.2.70.0050.00317.75
8.2.60.0030.00618.18
8.2.50.0070.00718.07
8.2.40.0050.00320.16
8.2.30.0000.00719.71
8.2.20.0050.00318.00
8.2.10.0070.00017.97
8.2.00.0040.00418.13
8.1.280.0070.00725.92
8.1.270.0040.00422.32
8.1.260.0080.00026.35
8.1.250.0060.00328.09
8.1.240.0090.00022.71
8.1.230.0040.00721.09
8.1.220.0050.00318.77
8.1.210.0000.00819.23
8.1.200.0040.00717.60
8.1.190.0060.00317.64
8.1.180.0080.00018.10
8.1.170.0050.00318.91
8.1.160.0040.00419.11
8.1.150.0000.00718.82
8.1.140.0000.00717.68
8.1.130.0070.00018.14
8.1.120.0040.00417.66
8.1.110.0000.00717.68
8.1.100.0040.00417.71
8.1.90.0040.00417.72
8.1.80.0000.00817.70
8.1.70.0090.00017.77
8.1.60.0060.00617.77
8.1.50.0000.00817.65
8.1.40.0040.00417.78
8.1.30.0050.00317.95
8.1.20.0040.00417.88
8.1.10.0000.00817.86
8.1.00.0040.00417.80
8.0.300.0040.00420.30
8.0.290.0000.00717.42
8.0.280.0000.00718.86
8.0.270.0070.00017.60
8.0.260.0000.00717.11
8.0.250.0040.00417.30
8.0.240.0000.00717.19
8.0.230.0000.00717.29
8.0.220.0000.00717.18
8.0.210.0000.00717.14
8.0.200.0000.00817.26
8.0.190.0040.00417.27
8.0.180.0000.00717.25
8.0.170.0080.00017.17
8.0.160.0080.00017.10
8.0.150.0000.00717.09
8.0.140.0080.00317.17
8.0.130.0060.00013.63
8.0.120.0030.00617.13
8.0.110.0080.00017.27
8.0.100.0070.00017.11
8.0.90.0070.00017.37
8.0.80.0060.00917.17
8.0.70.0070.00017.33
8.0.60.0000.00716.92
8.0.50.0050.00317.26
8.0.30.0070.01117.41
8.0.20.0120.00817.46
8.0.10.0040.00417.15
8.0.00.0050.01417.04
7.4.330.0050.00015.30
7.4.320.0030.00316.71
7.4.300.0000.00716.86
7.4.290.0070.00016.95
7.4.280.0050.00316.80
7.4.270.0030.00316.95
7.4.260.0050.00013.52
7.4.250.0000.00816.81
7.4.240.0000.00716.83
7.4.230.0070.00016.64
7.4.220.0110.00716.84
7.4.210.0090.00916.81
7.4.200.0000.00716.64
7.4.190.0000.00716.57
7.4.160.0050.01116.85
7.4.150.0090.00917.40
7.4.140.0060.01217.86
7.4.130.0150.00316.75
7.4.120.0160.00116.68
7.4.110.0090.00916.86
7.4.100.0030.01416.72
7.4.90.0110.00716.67
7.4.80.0080.01119.39
7.4.70.0040.01216.67
7.4.60.0140.00416.77
7.4.50.0030.00616.76
7.4.40.0040.01522.77
7.4.30.0060.01216.95
7.4.00.0030.01214.83
7.3.330.0000.00513.42
7.3.320.0030.00313.55
7.3.310.0040.00416.55
7.3.300.0020.00516.55
7.3.290.0070.00716.57
7.3.280.0080.01316.64
7.3.270.0130.01317.40
7.3.260.0130.00618.24
7.3.250.0100.01116.63
7.3.240.0080.00916.99
7.3.230.0090.00916.58
7.3.210.0070.01016.76
7.3.200.0140.00719.39
7.3.190.0050.01116.68
7.3.180.0110.01116.66
7.3.170.0090.00916.76
7.3.160.0030.01316.68
7.3.120.0060.00914.76
7.2.330.0120.00616.75
7.2.320.0200.00317.13
7.2.310.0070.01316.80
7.2.300.0090.00916.90
7.2.290.0070.01017.03
7.2.00.0030.01319.62
7.1.100.0060.00618.37
7.1.70.0060.00617.29
7.1.60.0110.01419.17
7.1.50.0210.00334.53
7.1.00.0000.05322.41
7.0.200.0020.00517.01
7.0.140.0030.07322.07
7.0.90.0470.05720.12
7.0.80.0400.05319.93
7.0.70.0370.05019.93
7.0.60.0470.03320.13
7.0.50.0330.05020.42
7.0.40.0000.04019.96
7.0.30.0070.03720.16
7.0.20.0030.07020.20
7.0.10.0070.03720.04
7.0.00.0000.07720.09
5.6.280.0070.07020.97
5.6.230.0030.04020.59
5.6.220.0070.05720.53
5.6.210.0000.04720.70
5.6.200.0030.04321.02
5.6.190.0170.05721.09
5.6.180.0030.03721.18
5.6.170.0100.05321.14
5.6.160.0030.05721.08
5.6.150.0070.03721.14
5.6.140.0130.03321.07
5.6.130.0030.04321.11
5.6.120.0070.04021.09
5.6.110.0070.04321.01
5.6.100.0030.03721.07
5.6.90.0070.06021.07
5.6.80.0170.07320.57
5.6.70.0130.05320.47
5.6.60.0030.05720.43
5.6.50.0030.05720.55
5.6.40.0030.06020.51
5.6.30.0100.06720.50
5.6.20.0170.07020.52
5.6.10.0130.04720.59
5.6.00.0070.08020.51
5.5.370.0000.04320.46
5.5.360.0030.06720.50
5.5.350.0000.04320.45
5.5.340.0070.07320.97
5.5.330.0030.06720.98
5.5.320.0070.04720.99
5.5.310.0030.04320.88
5.5.300.0130.03320.97
5.5.290.0030.05320.95
5.5.280.0000.04720.88
5.5.270.0070.07320.81
5.5.260.0100.05320.88
5.5.250.0030.04320.63
5.5.240.0200.08020.36
5.5.230.0100.08320.32
5.5.220.0070.07720.33
5.5.210.0000.08020.08
5.5.200.0100.07320.27
5.5.190.0070.06320.29
5.5.180.0070.07720.30
5.5.160.0030.06020.21
5.5.150.0030.06020.17
5.5.140.0130.07720.20
5.5.130.0170.04320.21
5.5.120.0130.07020.33
5.5.110.0030.08320.13
5.5.100.0200.06320.16
5.5.90.0100.05020.10
5.5.80.0030.04720.21
5.5.70.0130.05020.22
5.5.60.0070.04320.16
5.5.50.0000.04720.09
5.5.40.0000.08720.20
5.5.30.0130.04720.02
5.5.20.0070.05020.21
5.5.10.0030.08320.11
5.5.00.0100.07720.10
5.4.450.0030.03319.50
5.4.440.0030.04019.24
5.4.430.0030.06719.29
5.4.420.0070.05719.55
5.4.410.0030.04319.47
5.4.400.0170.06718.98
5.4.390.0070.04719.16
5.4.380.0130.04719.02
5.4.370.0070.07319.16
5.4.360.0100.07319.00
5.4.350.0200.06718.94
5.4.340.0130.07319.21
5.4.320.0030.07718.92
5.4.310.0100.06319.12
5.4.300.0130.07019.17
5.4.290.0030.04319.30
5.4.280.0070.03719.18
5.4.270.0000.05319.21
5.4.260.0170.05319.30
5.4.250.0200.06319.17
5.4.240.0100.06319.24
5.4.230.0070.08019.00
5.4.220.0170.06319.19
5.4.210.0030.04719.19
5.4.200.0130.05319.22
5.4.190.0030.04718.91
5.4.180.0070.07319.15
5.4.170.0070.08018.91
5.4.160.0100.07718.95
5.4.150.0130.06719.16
5.4.140.0070.07716.53
5.4.130.0030.07716.54
5.4.120.0030.04316.50
5.4.110.0130.06316.56
5.4.100.0000.04716.59
5.4.90.0100.03716.54
5.4.80.0000.04016.62
5.4.70.0070.06716.45
5.4.60.0070.07716.46
5.4.50.0070.04716.47
5.4.40.0100.04716.43
5.4.30.0070.07016.55
5.4.20.0100.06316.45
5.4.10.0030.05716.48
5.4.00.0100.04016.00
5.3.290.0070.09014.84
5.3.280.0130.05314.73
5.3.270.0100.07314.78
5.3.260.0070.05014.75
5.3.250.0100.07014.81
5.3.240.0030.05314.75
5.3.230.0070.05014.75
5.3.220.0100.04714.73
5.3.210.0000.06014.72
5.3.200.0030.06714.71
5.3.190.0130.06314.71
5.3.180.0070.04314.71
5.3.170.0130.03714.71
5.3.160.0100.07014.60
5.3.150.0170.04314.58
5.3.140.0000.05014.72
5.3.130.0130.07314.71
5.3.120.0030.06314.57
5.3.110.0130.06314.75
5.3.100.0070.07014.20
5.3.90.0070.07014.20
5.3.80.0130.07314.11
5.3.70.0200.05313.99
5.3.60.0100.07314.13
5.3.50.0130.06314.09
5.3.40.0100.07014.20
5.3.30.0030.05713.91
5.3.20.0170.06313.95
5.3.10.0030.07313.83
5.3.00.0100.07313.61
5.2.170.0030.06313.60
5.2.160.0030.03713.60
5.2.150.0100.04713.60
5.2.140.0100.05313.60
5.2.130.0130.05713.60
5.2.120.0070.04313.60
5.2.110.0030.05013.60
5.2.100.0070.06313.60
5.2.90.0030.06313.60
5.2.80.0100.05713.60
5.2.70.0030.04013.60
5.2.60.0070.03313.60
5.2.50.0000.05313.60
5.2.40.0000.06013.60
5.2.30.0070.04313.60
5.2.20.0070.03713.60
5.2.10.0000.05313.60
5.2.00.0070.04313.60
5.1.60.0000.04013.60
5.1.50.0070.03013.60
5.1.40.0070.04013.60
5.1.30.0070.05013.60
5.1.20.0030.05713.60
5.1.10.0170.04313.60
5.1.00.0030.05313.60
5.0.50.0000.03013.60
5.0.40.0030.03013.60
5.0.30.0000.07313.60
5.0.20.0030.03713.60
5.0.10.0070.04013.60
5.0.00.0000.07013.60
4.4.90.0100.02013.60
4.4.80.0070.03013.60
4.4.70.0070.03313.60
4.4.60.0030.02313.60
4.4.50.0100.03013.60
4.4.40.0030.03713.60
4.4.30.0030.03313.60
4.4.20.0000.03713.60
4.4.10.0000.02313.60
4.4.00.0030.05313.60
4.3.110.0000.01713.60
4.3.100.0030.01713.60
4.3.90.0070.03313.60
4.3.80.0000.04713.60
4.3.70.0030.03713.60
4.3.60.0000.03713.60
4.3.50.0000.03713.60
4.3.40.0030.04713.60
4.3.30.0000.04013.60
4.3.20.0000.03713.60
4.3.10.0030.03013.60
4.3.00.0000.03713.60

preferences:
44.35 ms | 401 KiB | 5 Q