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

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.70.0170.00016.88
8.3.60.0130.00618.58
8.3.50.0110.00921.34
8.3.40.0110.01118.96
8.3.30.0070.00720.27
8.3.20.0030.00520.73
8.3.10.0060.00321.32
8.3.00.0050.00323.91
8.2.180.0040.01117.00
8.2.170.0170.00322.96
8.2.160.0070.00720.62
8.2.150.0080.00024.18
8.2.140.0000.00724.66
8.2.130.0040.00426.16
8.2.120.0040.00421.19
8.2.110.0040.00720.79
8.2.100.0080.00418.16
8.2.90.0000.00719.45
8.2.80.0050.00219.65
8.2.70.0000.00917.71
8.2.60.0030.00618.41
8.2.50.0040.00418.07
8.2.40.0000.00820.20
8.2.30.0040.00419.74
8.2.20.0000.00917.85
8.2.10.0000.00817.90
8.2.00.0030.00518.07
8.1.280.0070.01425.92
8.1.270.0060.00323.99
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0070.00322.92
8.1.230.0070.00322.79
8.1.220.0040.00418.77
8.1.210.0030.00718.89
8.1.200.0000.00917.60
8.1.190.0030.00617.76
8.1.180.0040.00418.10
8.1.170.0080.00018.71
8.1.160.0000.00819.16
8.1.150.0030.00518.83
8.1.140.0040.00417.63
8.1.130.0030.00318.00
8.1.120.0040.00417.70
8.1.110.0040.00417.66
8.1.100.0070.00017.64
8.1.90.0000.00717.79
8.1.80.0050.00317.77
8.1.70.0000.00817.71
8.1.60.0080.00017.90
8.1.50.0000.00817.68
8.1.40.0040.00417.75
8.1.30.0000.00817.79
8.1.20.0030.00517.90
8.1.10.0050.00317.85
8.1.00.0000.00817.71
8.0.300.0080.00020.26
8.0.290.0050.00217.28
8.0.280.0000.00718.71
8.0.270.0070.00017.43
8.0.260.0030.00317.11
8.0.250.0040.00417.25
8.0.240.0000.00817.25
8.0.230.0000.00717.29
8.0.220.0070.00017.24
8.0.210.0000.00817.13
8.0.200.0060.00017.19
8.0.190.0080.00017.24
8.0.180.0070.00017.29
8.0.170.0030.00517.19
8.0.160.0040.00417.11
8.0.150.0040.00417.13
8.0.140.0070.00417.19
8.0.130.0000.00613.63
8.0.120.0040.00417.16
8.0.110.0000.00816.98
8.0.100.0070.00017.30
8.0.90.0040.00416.99
8.0.80.0100.01017.28
8.0.70.0080.00017.11
8.0.60.0000.00717.27
8.0.50.0050.00317.22
8.0.30.0140.00717.48
8.0.20.0110.00817.50
8.0.10.0080.00017.08
8.0.00.0090.01017.28
7.4.330.0000.00515.15
7.4.320.0040.00416.65
7.4.300.0000.00616.82
7.4.290.0000.00716.73
7.4.280.0030.00516.95
7.4.270.0030.00316.81
7.4.260.0000.00513.52
7.4.250.0050.00316.79
7.4.240.0000.00716.75
7.4.230.0030.00316.82
7.4.220.0030.01416.79
7.4.210.0040.01116.84
7.4.200.0000.00716.64
7.4.190.0040.00416.95
7.4.160.0090.00616.64
7.4.150.0100.00717.40
7.4.140.0110.01117.86
7.4.130.0110.00616.70
7.4.120.0090.00916.77
7.4.110.0110.00716.84
7.4.100.0090.00816.80
7.4.90.0120.00916.76
7.4.80.0100.01319.39
7.4.70.0090.00916.92
7.4.60.0050.01316.77
7.4.50.0060.00316.73
7.4.40.0060.01022.77
7.4.30.0090.01016.58
7.4.10.0070.01315.06
7.4.00.0020.01215.11
7.3.330.0000.00513.52
7.3.320.0050.00013.56
7.3.310.0070.00016.58
7.3.300.0000.00716.58
7.3.290.0080.00616.59
7.3.280.0060.01416.65
7.3.270.0140.00317.40
7.3.260.0120.00918.24
7.3.250.0100.00816.63
7.3.240.0070.01016.80
7.3.230.0080.00816.94
7.3.210.0170.00016.64
7.3.200.0090.01519.39
7.3.190.0130.00416.73
7.3.180.0170.00616.64
7.3.170.0120.00316.90
7.3.160.0110.00716.55
7.3.130.0060.00914.93
7.3.120.0100.00715.15
7.3.110.0060.01014.91
7.3.100.0050.00814.94
7.3.90.0070.00615.01
7.3.80.0040.00714.91
7.3.70.0070.00614.95
7.3.60.0020.00815.01
7.3.50.0020.00915.10
7.3.40.0060.00515.06
7.3.30.0050.00614.92
7.3.20.0020.00816.79
7.3.10.0100.00317.01
7.3.00.0060.00616.78
7.2.330.0100.00717.05
7.2.320.0100.01016.96
7.2.310.0080.01116.81
7.2.300.0130.00616.79
7.2.290.0030.01217.11
7.2.260.0070.01115.26
7.2.250.0070.01115.29
7.2.240.0080.00815.22
7.2.230.0070.00915.10
7.2.220.0050.01115.06
7.2.210.0030.01015.13
7.2.200.0020.01015.23
7.2.190.0070.00715.26
7.2.180.0080.00715.18
7.2.170.0040.00715.15
7.2.160.0050.00815.20
7.2.150.0070.00616.95
7.2.140.0060.00616.86
7.2.130.0100.00617.05
7.2.120.0060.00517.13
7.2.110.0070.00817.11
7.2.100.0000.01017.05
7.2.90.0100.00517.16
7.2.80.0080.00717.05
7.2.70.0060.00816.99
7.2.60.0060.00817.10
7.2.50.0080.00516.98
7.2.40.0020.01117.04
7.2.30.0060.00817.11
7.2.20.0070.00716.85
7.2.10.0040.00816.93
7.2.00.0040.01117.56
7.1.330.0060.00815.81
7.1.320.0080.00615.81
7.1.310.0050.00515.82
7.1.300.0050.00615.77
7.1.290.0050.00915.90
7.1.280.0050.00715.62
7.1.270.0030.00816.03
7.1.260.0050.01015.82
7.1.250.0050.00415.93
7.1.240.0050.00915.93
7.1.230.0030.00815.76
7.1.220.0040.00615.78
7.1.210.0080.00716.00
7.1.200.0100.00315.93
7.1.190.0030.01315.93
7.1.180.0040.01115.90
7.1.170.0050.00715.87
7.1.160.0030.01015.84
7.1.150.0040.00915.76
7.1.140.0040.00915.82
7.1.130.0070.00315.82
7.1.120.0060.00815.86
7.1.110.0070.00415.83
7.1.100.0050.00816.78
7.1.90.0030.00815.99
7.1.80.0070.00415.92
7.1.70.0100.00316.46
7.1.60.0080.00816.99
7.1.50.0080.00816.15
7.1.40.0020.00815.96
7.1.30.0070.00215.90
7.1.20.0050.00715.66
7.1.10.0050.01115.90
7.1.00.0040.01618.01
7.0.330.0080.00215.39
7.0.320.0090.00415.25
7.0.310.0070.00715.38
7.0.300.0070.00515.50
7.0.290.0050.00715.53
7.0.280.0030.00615.21
7.0.270.0060.00515.43
7.0.260.0040.00615.54
7.0.250.0040.00615.56
7.0.240.0080.00515.42
7.0.230.0050.00515.39
7.0.220.0030.01015.69
7.0.210.0030.00815.34
7.0.200.0010.01015.89
7.0.190.0050.00515.39
7.0.180.0070.00515.44
7.0.170.0030.00815.62
7.0.160.0090.00915.56
7.0.150.0050.00715.55
7.0.140.0020.03117.68
7.0.130.0040.00915.60
7.0.120.0040.00615.64
7.0.110.0030.00815.61
7.0.100.0060.03016.94
7.0.90.0150.03016.89
7.0.80.0160.02716.93
7.0.70.0160.02916.82
7.0.60.0120.02316.91
7.0.50.0060.03117.13
7.0.40.0020.03615.67
7.0.30.0060.02615.73
7.0.20.0070.03115.75
7.0.10.0080.02715.58
7.0.00.0080.02915.70
5.6.400.0040.00714.42
5.6.390.0060.01014.51
5.6.380.0060.00414.53
5.6.370.0030.00614.45
5.6.360.0030.00714.62
5.6.350.0050.00714.63
5.6.340.0050.00814.53
5.6.330.0120.00314.65
5.6.320.0090.00414.63
5.6.310.0020.01114.51
5.6.300.0090.00514.47
5.6.290.0040.00814.30
5.6.280.0050.02816.89
5.6.270.0100.00414.33
5.6.260.0080.00514.50
5.6.250.0040.02416.61
5.6.240.0040.03016.49
5.6.230.0030.02716.61
5.6.220.0060.03116.52
5.6.210.0050.03116.54
5.6.200.0060.03116.66
5.6.190.0020.02516.67
5.6.180.0050.03316.77
5.6.170.0080.02816.73
5.6.160.0030.03316.77
5.6.150.0060.03416.88
5.6.140.0070.02516.70
5.6.130.0030.03216.71
5.6.120.0080.03116.86
5.6.110.0060.03016.57
5.6.100.0070.03116.65
5.6.90.0080.02616.62
5.6.80.0060.03216.45
5.6.70.0080.02716.51
5.6.60.0070.03016.37
5.6.50.0040.03516.43
5.6.40.0050.03116.58
5.6.30.0110.02016.49
5.6.20.0050.02516.48
5.6.10.0050.02816.40
5.6.00.0070.03016.44
5.5.380.0030.02115.61
5.5.370.0050.02915.41
5.5.360.0080.02415.37
5.5.350.0060.03015.25
5.5.340.0080.02915.64
5.5.330.0090.02815.57
5.5.320.0080.03115.42
5.5.310.0040.03115.53
5.5.300.0090.03015.66
5.5.290.0070.03115.58
5.5.280.0050.02015.61
5.5.270.0090.02815.71
5.5.260.0050.03315.71
5.5.250.0040.03115.40
5.5.240.0070.02915.45
5.5.230.0090.02515.36
5.5.220.0020.01815.36
5.5.210.0060.02915.35
5.5.200.0040.03215.29
5.5.190.0090.02715.33
5.5.180.0080.01515.26
5.5.170.0060.00512.92
5.5.160.0050.02415.07
5.5.150.0050.02515.25
5.5.140.0070.01815.41
5.5.130.0030.03015.42
5.5.120.0080.02615.39
5.5.110.0080.01815.22
5.5.100.0050.02215.16
5.5.90.0100.02915.32
5.5.80.0050.03215.20
5.5.70.0090.02115.21
5.5.60.0070.01615.45
5.5.50.0100.02515.32
5.5.40.0100.02615.25
5.5.30.0060.02915.27
5.5.20.0090.02515.25
5.5.10.0100.02915.21
5.5.00.0030.02115.29
5.4.450.0060.02714.60
5.4.440.0060.02714.52
5.4.430.0030.03214.39
5.4.420.0030.03114.52
5.4.410.0020.03514.57
5.4.400.0060.02714.33
5.4.390.0040.02114.34
5.4.380.0090.02014.40
5.4.370.0040.02014.29
5.4.360.0050.02614.40
5.4.350.0080.02914.34
5.4.340.0040.03014.33
5.4.330.0030.00411.91
5.4.320.0050.02614.44
5.4.310.0030.02714.40
5.4.300.0030.02614.21
5.4.290.0030.02814.36
5.4.280.0060.01914.33
5.4.270.0060.02514.30
5.4.260.0040.02914.43
5.4.250.0050.03014.27
5.4.240.0030.02714.40
5.4.230.0040.03114.36
5.4.220.0050.03114.40
5.4.210.0050.02914.36
5.4.200.0060.03014.33
5.4.190.0020.02414.27
5.4.180.0030.03114.44
5.4.170.0090.01514.27
5.4.160.0050.02014.35
5.4.150.0080.02514.41
5.4.140.0050.03113.50
5.4.130.0070.02613.45
5.4.120.0050.03013.52
5.4.110.0050.02913.38
5.4.100.0050.01713.54
5.4.90.0030.03013.53
5.4.80.0070.02813.44
5.4.70.0050.02813.43
5.4.60.0020.03213.37
5.4.50.0040.01913.49
5.4.40.0080.02913.44
5.4.30.0040.01813.48
5.4.20.0050.02813.47
5.4.10.0110.02413.52
5.4.00.0080.02513.30
5.3.290.0050.02512.89
5.3.280.0030.01912.70
5.3.270.0020.02312.80
5.3.260.0020.02512.75
5.3.250.0080.02612.74
5.3.240.0060.01712.72
5.3.230.0010.02412.73
5.3.220.0080.02612.63
5.3.210.0100.01412.71
5.3.200.0040.02212.68
5.3.190.0070.02612.68
5.3.180.0040.02812.78
5.3.170.0050.02812.73
5.3.160.0090.02212.66
5.3.150.0070.02612.79
5.3.140.0060.02912.73
5.3.130.0020.02812.66
5.3.120.0050.02312.70
5.3.110.0030.02312.72
5.3.100.0070.02712.57
5.3.90.0060.01712.52
5.3.80.0020.02812.54
5.3.70.0070.02812.52
5.3.60.0040.02712.57
5.3.50.0050.01312.43
5.3.40.0070.03012.55
5.3.30.0040.02112.54
5.3.20.0060.02412.41
5.3.10.0070.02512.31
5.3.00.0040.01812.30
5.2.170.0030.03310.76
5.2.160.0030.02510.72
5.2.150.0030.02610.67
5.2.140.0020.02110.73
5.2.130.0080.02810.68
5.2.120.0030.02910.61
5.2.110.0070.03010.61
5.2.100.0020.03210.69
5.2.90.0040.03010.71
5.2.80.0030.03210.61
5.2.70.0070.03810.73
5.2.60.0030.03410.60
5.2.50.0060.02710.66
5.2.40.0080.02810.70
5.2.30.0020.03210.58
5.2.20.0030.02010.67
5.2.10.0070.02510.59
5.2.00.0050.02710.47
5.1.60.0040.02910.12
5.1.50.0040.02710.17
5.1.40.0030.02810.16
5.1.30.0030.02810.31
5.1.20.0070.02710.37
5.1.10.0020.03210.19
5.1.00.0050.02510.20
5.0.50.0060.0239.40
5.0.40.0050.0139.39
5.0.30.0030.0369.33
5.0.20.0020.0269.33
5.0.10.0060.0229.33
5.0.00.0020.0309.33
4.4.90.0000.0129.33
4.4.80.0010.0209.33
4.4.70.0020.0109.33
4.4.60.0020.0089.33
4.4.50.0030.0079.33
4.4.40.0030.0239.33
4.4.30.0020.0139.33
4.4.20.0020.0209.33
4.4.10.0030.0159.33
4.4.00.0030.0169.33
4.3.110.0030.0139.33
4.3.100.0010.0119.33
4.3.90.0050.0109.33
4.3.80.0000.0299.33
4.3.70.0020.0189.33
4.3.60.0030.0179.33
4.3.50.0030.0159.33
4.3.40.0020.0239.33
4.3.30.0000.0129.33
4.3.20.0000.0129.33
4.3.10.0050.0159.33
4.3.00.0000.0189.33

preferences:
44.82 ms | 401 KiB | 5 Q