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

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.0150.00018.55
8.3.50.0100.01021.33
8.3.40.0040.01119.08
8.3.30.0080.00820.46
8.3.20.0060.00320.78
8.3.10.0080.00021.36
8.3.00.0070.00023.70
8.2.180.0160.00016.72
8.2.170.0140.00722.96
8.2.160.0090.00620.68
8.2.150.0080.00024.18
8.2.140.0000.00824.66
8.2.130.0000.00726.16
8.2.120.0040.00421.36
8.2.110.0040.00722.49
8.2.100.0070.00418.16
8.2.90.0040.00418.09
8.2.80.0050.00317.97
8.2.70.0000.00817.88
8.2.60.0080.00018.15
8.2.50.0050.00318.07
8.2.40.0020.00518.47
8.2.30.0050.00219.62
8.2.20.0020.00517.88
8.2.10.0030.00517.93
8.2.00.0000.00818.01
8.1.280.0180.00625.92
8.1.270.0000.00823.99
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0000.00922.30
8.1.230.0030.01021.07
8.1.220.0000.00817.91
8.1.210.0000.00819.10
8.1.200.0000.01017.60
8.1.190.0080.00017.88
8.1.180.0050.00318.10
8.1.170.0040.00418.74
8.1.160.0050.00219.06
8.1.150.0000.00818.86
8.1.140.0000.00817.69
8.1.130.0000.00818.17
8.1.120.0040.00417.70
8.1.110.0040.00417.61
8.1.100.0040.00417.70
8.1.90.0040.00417.69
8.1.80.0000.00717.66
8.1.70.0030.00317.63
8.1.60.0040.00417.77
8.1.50.0000.00717.81
8.1.40.0040.00417.84
8.1.30.0040.00417.93
8.1.20.0080.00017.89
8.1.10.0000.00817.81
8.1.00.0040.00417.77
8.0.300.0040.00420.41
8.0.290.0000.00817.43
8.0.280.0040.00418.65
8.0.270.0040.00417.52
8.0.260.0070.00017.18
8.0.250.0070.00017.22
8.0.240.0030.00617.32
8.0.230.0040.00417.21
8.0.220.0000.00817.16
8.0.210.0030.00317.27
8.0.200.0000.00817.24
8.0.190.0030.00617.24
8.0.180.0000.00817.18
8.0.170.0000.00717.21
8.0.160.0000.00717.25
8.0.150.0070.00017.09
8.0.140.0060.00617.16
8.0.130.0030.00313.77
8.0.120.0040.00417.24
8.0.110.0080.00017.10
8.0.100.0000.00817.02
8.0.90.0030.00517.13
8.0.80.0060.01217.11
8.0.70.0040.00417.02
8.0.60.0050.00317.08
8.0.50.0040.00417.25
8.0.30.0070.01217.18
8.0.20.0090.01517.46
8.0.10.0070.00017.04
8.0.00.0140.00317.06
7.4.330.0050.00015.36
7.4.320.0040.00416.79
7.4.300.0030.00316.84
7.4.290.0040.00416.87
7.4.280.0000.00816.74
7.4.270.0000.00716.66
7.4.260.0000.00513.54
7.4.250.0080.00016.80
7.4.240.0040.00416.84
7.4.230.0000.00816.63
7.4.220.0090.01016.93
7.4.210.0120.00516.90
7.4.200.0040.00416.91
7.4.190.0040.00417.07
7.4.160.0080.00816.70
7.4.150.0170.00317.40
7.4.140.0140.00617.86
7.4.130.0090.00816.57
7.4.120.0110.00616.82
7.4.110.0100.00616.57
7.4.100.0110.00716.67
7.4.90.0160.00316.71
7.4.80.0130.00719.39
7.4.70.0080.01016.92
7.4.60.0060.01116.78
7.4.50.0060.00316.82
7.4.40.0080.00522.77
7.4.30.0070.01416.86
7.4.10.0070.01315.11
7.4.00.0080.00715.13
7.3.330.0000.00613.52
7.3.320.0000.00513.54
7.3.310.0030.00316.63
7.3.300.0030.00316.61
7.3.290.0140.00016.61
7.3.280.0100.00916.58
7.3.270.0080.00817.40
7.3.260.0130.00918.24
7.3.250.0140.00516.70
7.3.240.0130.00316.63
7.3.230.0110.00816.70
7.3.210.0090.00916.82
7.3.200.0200.00319.39
7.3.190.0060.01616.76
7.3.180.0160.00716.72
7.3.170.0110.00616.85
7.3.160.0100.00716.70
7.3.130.0100.01014.69
7.3.120.0080.01015.00
7.3.110.0050.01514.98
7.3.100.0080.00615.00
7.3.90.0070.01014.83
7.3.80.0020.01014.84
7.3.70.0030.00614.80
7.3.60.0030.01214.95
7.3.50.0070.00814.87
7.3.40.0030.00715.03
7.3.30.0070.00314.81
7.3.20.0090.00716.66
7.3.10.0040.01116.70
7.3.00.0050.01016.77
7.2.330.0070.01117.01
7.2.320.0120.01116.77
7.2.310.0170.00216.80
7.2.300.0120.00617.14
7.2.290.0100.00616.96
7.2.260.0090.01215.20
7.2.250.0080.01015.25
7.2.240.0110.00515.21
7.2.230.0040.01215.07
7.2.220.0020.01215.21
7.2.210.0050.00815.18
7.2.200.0060.00615.13
7.2.190.0070.00715.23
7.2.180.0090.00315.19
7.2.170.0060.01215.33
7.2.160.0090.00615.44
7.2.150.0070.00717.00
7.2.140.0060.00317.09
7.2.130.0060.00717.05
7.2.120.0030.01017.00
7.2.110.0060.00716.93
7.2.100.0050.00916.81
7.2.90.0050.00717.09
7.2.80.0030.01217.10
7.2.70.0050.00717.23
7.2.60.0050.00917.04
7.2.50.0070.00517.04
7.2.40.0090.00717.19
7.2.30.0050.00917.04
7.2.20.0060.00516.95
7.2.10.0030.00616.91
7.2.00.0050.01017.83
7.1.330.0080.00515.92
7.1.320.0050.00815.88
7.1.310.0050.01015.86
7.1.300.0050.01015.80
7.1.290.0090.00515.74
7.1.280.0060.00815.90
7.1.270.0000.01015.80
7.1.260.0030.01015.85
7.1.250.0090.00515.91
7.1.240.0070.00315.93
7.1.230.0060.00615.94
7.1.220.0040.00715.89
7.1.210.0130.00316.16
7.1.200.0090.00315.91
7.1.190.0070.00715.88
7.1.180.0070.00715.71
7.1.170.0040.00715.99
7.1.160.0060.00915.92
7.1.150.0080.00315.71
7.1.140.0090.00615.70
7.1.130.0030.00916.00
7.1.120.0130.00315.88
7.1.110.0070.00415.88
7.1.100.0070.00617.18
7.1.90.0060.00616.02
7.1.80.0090.00016.12
7.1.70.0120.00616.25
7.1.60.0080.00817.40
7.1.50.0100.00816.50
7.1.40.0030.01015.93
7.1.30.0100.00315.82
7.1.20.0000.01515.94
7.1.10.0030.00615.59
7.1.00.0050.01819.18
7.0.330.0120.00315.58
7.0.320.0040.00815.59
7.0.310.0040.00715.32
7.0.300.0030.00715.64
7.0.290.0000.01115.36
7.0.280.0000.00815.70
7.0.270.0110.00815.61
7.0.260.0030.01315.25
7.0.250.0080.00815.73
7.0.240.0040.00815.45
7.0.230.0120.00315.49
7.0.220.0000.01015.72
7.0.210.0000.01815.24
7.0.200.0020.00916.19
7.0.190.0080.00815.58
7.0.180.0040.00915.47
7.0.170.0060.00915.68
7.0.160.0000.01715.63
7.0.150.0040.00715.27
7.0.140.0000.04418.76
7.0.130.0080.00815.60
7.0.120.0040.00415.69
7.0.110.0070.00715.46
7.0.100.0030.02517.74
7.0.90.0030.02117.81
7.0.80.0050.02917.86
7.0.70.0050.02217.68
7.0.60.0050.02517.78
7.0.50.0030.02718.04
7.0.40.0020.02716.81
7.0.30.0050.01916.79
7.0.20.0050.04316.86
7.0.10.0050.04116.81
7.0.00.0030.03816.88
5.6.400.0000.01414.38
5.6.390.0040.00714.26
5.6.380.0030.01114.76
5.6.370.0030.00914.52
5.6.360.0030.00614.50
5.6.350.0000.00914.52
5.6.340.0030.00614.66
5.6.330.0080.00414.86
5.6.320.0090.00314.65
5.6.310.0040.00814.78
5.6.300.0030.00914.92
5.6.290.0100.00314.88
5.6.280.0030.04017.89
5.6.270.0030.01014.54
5.6.260.0120.00014.62
5.6.250.0070.02317.77
5.6.240.0090.02117.63
5.6.230.0060.02317.71
5.6.220.0030.02617.69
5.6.210.0060.02317.51
5.6.200.0050.02317.77
5.6.190.0100.01717.79
5.6.180.0000.02617.82
5.6.170.0070.01817.87
5.6.160.0070.04017.81
5.6.150.0030.02318.00
5.6.140.0050.04717.74
5.6.130.0110.03317.89
5.6.120.0020.04017.72
5.6.110.0100.02818.00
5.6.100.0100.04017.83
5.6.90.0040.02817.68
5.6.80.0050.02717.51
5.6.70.0070.02017.54
5.6.60.0050.03817.37
5.6.50.0100.03717.49
5.6.40.0070.02817.40
5.6.30.0080.04417.50
5.6.20.0050.04517.53
5.6.10.0050.02517.53
5.6.00.0070.02317.48
5.5.380.0050.02517.38
5.5.370.0030.02117.62
5.5.360.0080.02017.38
5.5.350.0080.02317.50
5.5.340.0060.02317.68
5.5.330.0070.02217.55
5.5.320.0140.01417.81
5.5.310.0090.02317.79
5.5.300.0120.03917.86
5.5.290.0060.02617.74
5.5.280.0070.04217.77
5.5.270.0060.02917.65
5.5.260.0070.03717.65
5.5.250.0050.02717.44
5.5.240.0120.03817.43
5.5.230.0050.03017.29
5.5.220.0050.04017.30
5.5.210.0050.03917.28
5.5.200.0050.04617.30
5.5.190.0080.03817.50
5.5.180.0100.03317.32
5.5.170.0120.00314.09
5.5.160.0060.02717.47
5.5.150.0060.02517.35
5.5.140.0070.04017.13
5.5.130.0140.03317.46
5.5.120.0100.03217.26
5.5.110.0020.04417.17
5.5.100.0040.03917.29
5.5.90.0070.03817.40
5.5.80.0030.02517.35
5.5.70.0080.02817.27
5.5.60.0080.03317.38
5.5.50.0030.03817.28
5.5.40.0050.02317.21
5.5.30.0070.03717.26
5.5.20.0060.02517.20
5.5.10.0030.03717.14
5.5.00.0030.03217.35
5.4.450.0050.02815.65
5.4.440.0040.03915.77
5.4.430.0040.04715.80
5.4.420.0060.04315.77
5.4.410.0030.02915.61
5.4.400.0100.03015.60
5.4.390.0020.03015.59
5.4.380.0100.03315.62
5.4.370.0080.02515.49
5.4.360.0050.03815.47
5.4.350.0100.02115.48
5.4.340.0050.04815.62
5.4.330.0090.00312.01
5.4.320.0090.04015.65
5.4.310.0060.02415.51
5.4.300.0070.03315.56
5.4.290.0050.03715.61
5.4.280.0020.02315.58
5.4.270.0020.04315.64
5.4.260.0020.02315.55
5.4.250.0030.04015.58
5.4.240.0030.03515.46
5.4.230.0060.02115.61
5.4.220.0040.03715.61
5.4.210.0070.03115.61
5.4.200.0080.03515.46
5.4.190.0030.04415.66
5.4.180.0090.03815.64
5.4.170.0120.02215.56
5.4.160.0060.03115.58
5.4.150.0050.02515.53
5.4.140.0020.02914.27
5.4.130.0070.02314.29
5.4.120.0050.04014.27
5.4.110.0040.03714.29
5.4.100.0080.02214.32
5.4.90.0050.02614.28
5.4.80.0030.03914.23
5.4.70.0030.02914.24
5.4.60.0030.04614.27
5.4.50.0060.03714.29
5.4.40.0050.03614.30
5.4.30.0050.04014.30
5.4.20.0060.03414.22
5.4.10.0030.02814.28
5.4.00.0030.03213.88
5.3.290.0030.04313.42
5.3.280.0020.02513.38
5.3.270.0000.03213.42
5.3.260.0130.03213.42
5.3.250.0050.03613.34
5.3.240.0000.02713.37
5.3.230.0060.02513.31
5.3.220.0040.02513.37
5.3.210.0070.03513.29
5.3.200.0030.03013.40
5.3.190.0050.02213.37
5.3.180.0030.02313.31
5.3.170.0100.02013.30
5.3.160.0070.03613.31
5.3.150.0070.02313.34
5.3.140.0090.02513.37
5.3.130.0100.02213.29
5.3.120.0050.04513.29
5.3.110.0080.04313.41
5.3.100.0050.04113.11
5.3.90.0070.02213.02
5.3.80.0090.03613.12
5.3.70.0060.02113.10
5.3.60.0040.02713.08
5.3.50.0050.02913.08
5.3.40.0050.03713.09
5.3.30.0100.03313.06
5.3.20.0050.04012.93
5.3.10.0070.01812.84
5.3.00.0000.03012.83
5.2.170.0000.04012.53
5.2.160.0100.04312.53
5.2.150.0030.03312.53
5.2.140.0030.03712.53
5.2.130.0100.06012.53
5.2.120.0000.03712.53
5.2.110.0000.03712.53
5.2.100.0030.03012.53
5.2.90.0000.03312.53
5.2.80.0000.06712.53
5.2.70.0100.04312.53
5.2.60.0030.04312.53
5.2.50.0000.05312.53
5.2.40.0030.05712.53
5.2.30.0100.02712.53
5.2.20.0070.02712.53
5.2.10.0070.06012.53
5.2.00.0070.05712.53
5.1.60.0070.05012.53
5.1.50.0000.05312.53
5.1.40.0070.02712.53
5.1.30.0000.05012.53
5.1.20.0000.04712.53
5.1.10.0030.03012.53
5.1.00.0000.06012.53
5.0.50.0000.03312.53
5.0.40.0030.04012.53
5.0.30.0070.05712.53
5.0.20.0000.02012.53
5.0.10.0030.04312.53
5.0.00.0070.04312.53
4.4.90.0030.02012.53
4.4.80.0030.02712.53
4.4.70.0070.02712.53
4.4.60.0070.03012.53
4.4.50.0000.02312.53
4.4.40.0000.03712.53
4.4.30.0030.02312.53
4.4.20.0000.03712.53
4.4.10.0000.03012.53
4.4.00.0030.03312.53
4.3.110.0000.03312.53
4.3.100.0000.01712.53
4.3.90.0000.02312.53
4.3.80.0100.03312.53
4.3.70.0030.02012.53
4.3.60.0100.02712.53
4.3.50.0030.03312.53
4.3.40.0030.05312.53
4.3.30.0000.03312.53
4.3.20.0000.03712.53
4.3.10.0030.03312.53
4.3.00.0030.02312.53

preferences:
47.05 ms | 401 KiB | 5 Q