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

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.0140.00418.55
8.3.50.0020.01716.68
8.3.40.0040.01119.08
8.3.30.0110.00718.82
8.3.20.0040.00418.98
8.3.10.0040.00420.73
8.3.00.0090.00619.63
8.2.180.0110.00717.13
8.2.170.0070.00722.96
8.2.160.0150.00022.38
8.2.150.0040.00424.18
8.2.140.0080.00024.66
8.2.130.0000.00826.16
8.2.120.0030.00520.02
8.2.110.0050.00521.29
8.2.100.0080.00418.05
8.2.90.0040.00419.46
8.2.80.0060.00317.97
8.2.70.0050.00317.75
8.2.60.0040.00418.28
8.2.50.0080.00018.16
8.2.40.0040.00720.77
8.2.30.0030.00518.30
8.2.20.0000.00818.00
8.2.10.0030.00517.86
8.2.00.0080.00017.86
8.1.280.0120.00925.92
8.1.270.0080.00023.99
8.1.260.0000.00826.35
8.1.250.0070.00028.09
8.1.240.0060.00322.27
8.1.230.0060.00621.07
8.1.220.0040.00418.04
8.1.210.0040.00418.77
8.1.200.0000.01017.47
8.1.190.0050.00517.60
8.1.180.0080.00018.10
8.1.170.0000.00818.97
8.1.160.0040.00419.06
8.1.150.0040.00418.80
8.1.140.0000.01017.81
8.1.130.0000.00718.10
8.1.120.0040.00417.69
8.1.110.0000.00917.70
8.1.100.0000.00817.71
8.1.90.0070.00317.77
8.1.80.0030.00617.73
8.1.70.0030.00317.68
8.1.60.0030.00517.86
8.1.50.0030.00517.66
8.1.40.0000.00817.76
8.1.30.0000.00817.80
8.1.20.0030.00517.89
8.1.10.0060.00317.85
8.1.00.0050.00317.73
8.0.300.0000.00718.77
8.0.290.0040.00417.00
8.0.280.0030.00318.75
8.0.270.0040.00417.66
8.0.260.0030.00318.80
8.0.250.0040.00417.24
8.0.240.0060.00617.18
8.0.230.0070.00017.16
8.0.220.0070.00017.28
8.0.210.0000.00717.13
8.0.200.0040.00417.26
8.0.190.0000.00817.26
8.0.180.0000.00717.14
8.0.170.0050.00317.14
8.0.160.0000.00717.27
8.0.150.0000.00817.06
8.0.140.0030.00717.11
8.0.130.0000.00513.61
8.0.120.0000.00717.15
8.0.110.0040.00417.18
8.0.100.0050.00317.30
8.0.90.0050.00317.13
8.0.80.0090.00617.13
8.0.70.0000.00817.06
8.0.60.0000.00717.24
8.0.50.0040.00417.32
8.0.30.0110.00817.51
8.0.20.0100.00917.44
8.0.10.0040.00417.25
8.0.00.0120.00717.00
7.4.330.0000.00515.05
7.4.320.0000.00716.70
7.4.300.0000.00716.95
7.4.290.0050.00316.74
7.4.280.0070.00916.86
7.4.270.0000.00716.91
7.4.260.0040.00413.42
7.4.250.0040.00416.93
7.4.240.0070.00016.83
7.4.230.0030.00316.91
7.4.220.0100.01016.59
7.4.210.0090.00716.79
7.4.200.0040.00416.85
7.4.190.0000.00716.86
7.4.160.0130.00316.75
7.4.150.0070.01117.07
7.4.140.0080.01117.34
7.4.130.0070.01216.74
7.4.120.0080.01116.74
7.4.110.0030.01516.85
7.4.100.0070.01416.83
7.4.90.0070.01116.77
7.4.80.0160.00418.80
7.4.70.0100.01016.55
7.4.60.0040.01316.64
7.4.50.0000.00716.95
7.4.40.0050.00522.05
7.4.30.0090.00616.54
7.4.00.0040.01315.18
7.3.330.0000.00613.60
7.3.320.0070.00013.41
7.3.310.0040.00416.60
7.3.300.0070.00016.55
7.3.290.0120.00316.64
7.3.280.0100.00816.66
7.3.270.0110.00817.07
7.3.260.0080.00918.24
7.3.250.0060.01116.51
7.3.240.0060.01616.90
7.3.230.0060.00916.83
7.3.210.0000.01816.81
7.3.200.0110.01119.39
7.3.190.0100.00716.63
7.3.180.0000.01716.82
7.3.170.0030.01416.77
7.3.160.0090.00616.88
7.3.120.0080.00815.06
7.3.110.0110.00714.82
7.3.100.0070.01015.08
7.3.90.0060.00615.20
7.3.80.0070.00714.98
7.3.70.0100.00314.88
7.3.60.0080.00014.92
7.3.50.0070.00714.82
7.3.40.0090.00915.13
7.3.30.0060.00614.92
7.3.20.0060.00616.89
7.3.10.0090.00016.66
7.3.00.0100.00317.02
7.2.330.0110.00717.09
7.2.320.0190.00817.05
7.2.310.0140.00716.77
7.2.300.0100.00717.02
7.2.290.0100.00617.13
7.2.250.0120.00915.11
7.2.240.0100.01015.26
7.2.230.0060.00315.39
7.2.220.0070.00715.49
7.2.210.0030.00915.37
7.2.200.0040.00715.23
7.2.190.0040.00815.24
7.2.180.0000.01415.09
7.2.170.0060.00915.30
7.2.00.0030.01019.64
7.1.330.0030.01015.83
7.1.320.0080.00416.12
7.1.310.0080.00415.75
7.1.300.0080.00315.59
7.1.290.0090.00615.91
7.1.280.0070.00715.80
7.1.270.0090.00316.16
7.1.260.0080.00615.82
7.1.100.0100.00018.29
7.1.70.0040.00417.14
7.1.60.0130.01319.40
7.1.50.0100.01316.98
7.1.00.0030.07322.52
7.0.200.0060.00316.60
7.0.140.0000.07022.03
7.0.110.0570.03320.07
7.0.100.0500.06320.12
7.0.90.0500.06720.09
7.0.80.0470.04020.00
7.0.70.0400.03020.16
7.0.60.0600.05719.99
7.0.50.0370.04020.03
7.0.40.0030.05019.81
7.0.30.0070.03319.76
7.0.20.0100.03019.85
7.0.10.0070.04319.83
7.0.00.0100.05319.81
5.6.280.0000.07020.96
5.6.260.0070.07020.78
5.6.250.0070.04720.80
5.6.240.0030.05020.59
5.6.230.0070.04320.61
5.6.220.0030.03720.73
5.6.210.0030.04020.69
5.6.200.0030.08320.71
5.6.190.0170.07720.59
5.6.180.0100.06320.68
5.6.170.0030.05020.74
5.6.160.0100.06020.59
5.6.150.0070.04020.58
5.6.140.0000.04320.70
5.6.130.0070.03320.90
5.6.120.0030.05320.67
5.6.110.0100.05020.72
5.6.100.0100.03720.70
5.6.90.0000.04320.69
5.6.80.0030.04020.14
5.6.70.0000.04720.09
5.6.60.0100.07320.05
5.6.50.0170.06720.06
5.6.40.0100.05720.12
5.6.30.0100.03320.11
5.6.20.0070.03720.26
5.6.10.0070.04020.03
5.6.00.0070.06720.02
5.5.380.0070.06017.59
5.5.370.0070.05717.63
5.5.360.0030.04017.72
5.5.350.0100.03017.55
5.5.340.0000.03718.16
5.5.330.0070.03018.20
5.5.320.0000.03718.20
5.5.310.0100.04318.30
5.5.300.0100.06718.11
5.5.290.0030.07718.29
5.5.280.0100.03017.98
5.5.270.0070.05018.01
5.5.260.0000.05018.38
5.5.250.0030.04018.04
5.5.240.0000.04017.38
5.5.230.0000.03317.38
5.5.220.0000.05017.37
5.5.210.0070.03317.33
5.5.200.0000.03317.58
5.5.190.0030.06017.65
5.5.180.0070.03717.41
5.5.160.0130.03017.58
5.5.150.0030.06017.50
5.5.140.0070.06717.37
5.5.130.0100.05017.54
5.5.120.0030.03717.64
5.5.110.0000.04017.57
5.5.100.0000.04317.46
5.5.90.0030.05317.54
5.5.80.0070.03717.23
5.5.70.0030.03017.24
5.5.60.0130.02717.27
5.5.50.0030.03717.24
5.5.40.0070.03317.26
5.5.30.0070.03017.62
5.5.20.0000.04017.15
5.5.10.0030.03717.28
5.5.00.0070.03317.54
5.4.450.0030.04019.58
5.4.440.0000.06019.34
5.4.430.0030.03719.53
5.4.420.0030.07019.64
5.4.410.0070.03719.27
5.4.400.0030.03719.17
5.4.390.0030.03319.45
5.4.380.0070.06019.30
5.4.370.0000.04019.16
5.4.360.0030.05719.21
5.4.350.0070.03319.45
5.4.340.0030.04719.03
5.4.320.0000.04319.40
5.4.310.0000.04319.10
5.4.300.0030.03719.16
5.4.290.0070.06019.31
5.4.280.0000.04019.07
5.4.270.0070.04719.14
5.4.260.0000.04019.10
5.4.250.0100.03019.12
5.4.240.0070.03319.29
5.4.230.0030.03319.10
5.4.220.0070.03019.01
5.4.210.0000.03719.43
5.4.200.0000.03319.22
5.4.190.0030.04019.22
5.4.180.0000.03319.07
5.4.170.0030.03019.05
5.4.160.0070.02719.13
5.4.150.0030.03019.21
5.4.140.0070.02716.66
5.4.130.0070.02716.43
5.4.120.0030.03016.65
5.4.110.0030.03016.48
5.4.100.0070.03016.73
5.4.90.0030.03016.63
5.4.80.0030.03016.73
5.4.70.0000.03716.46
5.4.60.0030.03016.59
5.4.50.0000.03016.75
5.4.40.0100.02316.70
5.4.30.0030.03016.62
5.4.20.0070.03716.62
5.4.10.0030.03716.59
5.4.00.0030.04316.10
5.3.290.0030.05314.68
5.3.280.0000.03314.60
5.3.270.0070.03314.70
5.3.260.0000.04014.61
5.3.250.0070.02714.82
5.3.240.0000.03714.70
5.3.230.0100.03314.68
5.3.220.0070.03014.74
5.3.210.0070.04314.68
5.3.200.0100.02714.64
5.3.190.0070.02714.64
5.3.180.0070.02714.67
5.3.170.0000.03714.71
5.3.160.0030.03014.68
5.3.150.0030.03014.65
5.3.140.0030.03014.73
5.3.130.0070.03314.71
5.3.120.0030.03714.71
5.3.110.0030.03014.68
5.3.100.0030.03014.35
5.3.90.0070.02714.06
5.3.80.0030.03014.22
5.3.70.0070.03014.16
5.3.60.0000.03314.08
5.3.50.0070.06713.99
5.3.40.0030.03013.95
5.3.30.0030.04014.07
5.3.20.0070.04014.00
5.3.10.0130.02713.97
5.3.00.0000.04713.72
5.2.170.0000.03712.52
5.2.160.0000.03012.52
5.2.150.0000.03312.52
5.2.140.0030.02712.52
5.2.130.0030.02712.52
5.2.120.0030.02712.52
5.2.110.0000.02712.52
5.2.100.0030.02312.52
5.2.90.0000.02712.52
5.2.80.0000.04012.52
5.2.70.0030.02312.52
5.2.60.0030.02312.52
5.2.50.0070.04312.52
5.2.40.0030.03012.52
5.2.30.0000.03012.52
5.2.20.0030.02712.52
5.2.10.0000.02312.52
5.2.00.0000.03012.52
5.1.60.0000.02312.52
5.1.50.0000.02012.52
5.1.40.0000.02012.52
5.1.30.0000.02712.52
5.1.20.0030.03312.52
5.1.10.0000.04312.52
5.1.00.0030.02712.52
5.0.50.0000.02712.52
5.0.40.0000.02012.52
5.0.30.0000.03312.52
5.0.20.0000.04012.52
5.0.10.0000.02012.52
5.0.00.0000.04012.52
4.4.90.0000.01312.52
4.4.80.0070.01312.52
4.4.70.0000.03312.52
4.4.60.0000.01312.52
4.4.50.0030.01012.52
4.4.40.0000.02012.52
4.4.30.0030.01012.52
4.4.20.0000.03712.52
4.4.10.0000.01712.52
4.4.00.0000.02312.52
4.3.110.0000.02012.52
4.3.100.0000.02312.52
4.3.90.0000.01312.52
4.3.80.0000.02712.52
4.3.70.0030.01312.52
4.3.60.0030.01012.52
4.3.50.0000.01312.52
4.3.40.0030.02012.52
4.3.30.0030.01712.52
4.3.20.0000.03712.52
4.3.10.0000.01312.52
4.3.00.0000.01312.52

preferences:
44.75 ms | 401 KiB | 5 Q