3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * DOM xpath to find #text nodes and wrap in paragraph tag * @link http://stackoverflow.com/q/15552560/367456 */ $html = '<div> This text should be wrapped in a p tag. </div> This also should be wrapped. <b>And</b> this.'; $paragraphSequence = "\n\n"; $paragraphSequenceLength = 2; $paragraphComment = 'break'; /** * @param DOMNode $element * * @link http://php.net/book.dom * @return string */ $innerHTML = function (DOMNode $element) { $innerHTML = ""; $children = $element->childNodes; foreach ($children as $child) { $innerHTML .= $element->ownerDocument->saveHTML($child); } return $innerHTML; }; /** * @param $html * * @return DOMElement * @throws InvalidArgumentException */ $loadHTMLFragment = function ($html) { $doc = new DOMDocument(); $doc->loadHTML("<body>$html"); $anchor = $doc->getElementsByTagName('body')->item(0); if (!$anchor) { throw new InvalidArgumentException('Unable to load HTML fragment.'); } return $anchor; }; $insertBreakMarkBefore = function (DOMText $node, $paragraphSequenceBefore = FALSE) use ($paragraphSequence, $paragraphComment) { if ($paragraphSequenceBefore) { $node->parentNode->insertBefore($node->ownerDocument->createTextNode($paragraphSequence), $node); } $node->parentNode->insertBefore($node->ownerDocument->createComment($paragraphComment), $node); $node->parentNode->insertBefore($node->ownerDocument->createTextNode($paragraphSequence), $node); }; $anchor = $loadHTMLFragment($html); $xp = new DOMXPath($anchor->ownerDocument); /* @var $result DOMText[] */ $result = $xp->query('(.|./div)/text()', $anchor); foreach ($result as $i => $node) { if ($node->parentNode->tagName == 'div') { $insertBreakMarkBefore($node, true); } while (FALSE !== $pos = strpos($node->data, $paragraphSequence)) { $node = $node->splitText($pos + $paragraphSequenceLength); $insertBreakMarkBefore($node); } } $needle = sprintf('%1$s<!--%2$s-->%1$s', $paragraphSequence, $paragraphComment); $replace = sprintf("\n<p class=\"%s\">\n", $paragraphComment); $html = strtr($innerHTML($anchor), array($needle . $needle => $replace, $needle => $replace)); echo "HTML afterwards:\n", $innerHTML($loadHTMLFragment($html));

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.40.0130.00619.05
8.3.30.0150.00318.96
8.3.20.0030.00619.23
8.3.10.0030.00619.07
8.3.00.0060.00319.76
8.2.170.0040.01122.96
8.2.160.0090.00622.81
8.2.150.0030.00624.18
8.2.140.0060.00324.66
8.2.130.0080.00026.16
8.2.120.0000.00821.16
8.2.110.0030.00620.83
8.2.100.0060.00618.22
8.2.90.0060.00318.13
8.2.80.0030.00519.16
8.2.70.0080.00418.00
8.2.60.0000.00918.43
8.2.50.0050.00318.07
8.2.40.0030.00619.86
8.2.30.0040.00418.04
8.2.20.0070.00417.95
8.2.10.0040.00418.20
8.2.00.0100.00318.12
8.1.270.0050.00323.99
8.1.260.0050.00326.35
8.1.250.0060.00328.09
8.1.240.0140.00019.32
8.1.230.0040.00818.20
8.1.220.0030.00618.14
8.1.210.0100.00018.77
8.1.200.0030.00717.86
8.1.190.0030.00517.63
8.1.180.0040.00418.10
8.1.170.0000.00919.38
8.1.160.0000.00719.29
8.1.150.0030.00519.11
8.1.140.0040.00417.84
8.1.130.0050.00319.55
8.1.120.0040.00417.82
8.1.110.0000.00817.87
8.1.100.0040.00417.87
8.1.90.0040.00417.85
8.1.80.0070.00017.88
8.1.70.0050.00217.78
8.1.60.0030.00517.99
8.1.50.0030.00517.86
8.1.40.0060.00317.92
8.1.30.0060.00317.91
8.1.20.0080.00018.00
8.1.10.0000.00917.87
8.1.00.0030.00617.96
8.0.300.0050.00318.77
8.0.290.0090.00017.13
8.0.280.0000.00718.96
8.0.270.0030.00417.52
8.0.260.0030.00317.23
8.0.250.0030.00517.30
8.0.240.0050.00317.41
8.0.230.0050.00217.29
8.0.220.0000.00817.32
8.0.210.0000.00717.31
8.0.200.0040.00417.36
8.0.190.0000.00717.38
8.0.180.0100.00017.35
8.0.170.0060.00317.20
8.0.160.0000.00717.33
8.0.150.0040.00417.37
8.0.140.0090.00317.29
8.0.130.0030.00313.79
8.0.120.0030.00617.21
8.0.110.0040.00417.33
8.0.100.0000.00817.16
8.0.90.0040.00417.22
8.0.80.0030.01217.29
8.0.70.0040.00417.46
8.0.60.0060.00317.26
8.0.50.0040.00417.24
8.0.30.0060.01517.66
8.0.20.0120.00817.55
8.0.10.0030.00517.36
8.0.00.0120.00817.19
7.4.330.0020.00213.30
7.4.320.0000.00716.95
7.4.300.0070.00016.98
7.4.290.0000.00716.87
7.4.280.0030.00616.77
7.4.270.0050.00216.90
7.4.260.0000.00513.48
7.4.250.0000.00816.97
7.4.240.0030.00316.90
7.4.230.0100.00017.07
7.4.220.0080.01116.95
7.4.210.0110.00916.75
7.4.200.0040.00417.07
7.4.190.0030.00516.74
7.4.160.0100.00616.86
7.4.150.0150.00416.75
7.4.140.0200.00416.84
7.4.130.0090.00916.64
7.4.120.0180.00616.85
7.4.110.0060.01216.70
7.4.100.0140.00516.69
7.4.90.0040.01316.75
7.4.80.0070.01319.39
7.4.70.0100.00716.88
7.4.60.0170.00016.80
7.4.50.0000.00517.04
7.4.40.0070.00716.96
7.4.30.0030.02016.80
7.4.00.0060.00914.96
7.3.330.0000.00713.69
7.3.320.0050.00013.61
7.3.310.0070.00016.56
7.3.300.0070.00016.70
7.3.290.0090.00816.67
7.3.280.0100.00816.69
7.3.270.0060.01216.82
7.3.260.0070.01016.76
7.3.250.0140.00716.88
7.3.240.0110.01416.56
7.3.230.0100.01017.00
7.3.210.0070.01116.77
7.3.200.0060.01116.86
7.3.190.0140.00317.02
7.3.180.0080.01016.66
7.3.170.0100.00716.86
7.3.160.0170.00316.98
7.3.120.0030.01214.76
7.2.330.0110.00717.07
7.2.320.0100.00716.80
7.2.310.0100.01317.13
7.2.300.0060.01117.13
7.2.290.0130.00516.89
7.1.100.0200.00918.24
7.1.70.0040.00417.26
7.1.60.0090.01519.30
7.1.50.0120.01217.21
7.1.00.0000.03722.54
7.0.200.0000.00816.85
7.0.140.0070.07322.16
7.0.60.0100.08720.20
7.0.50.0070.04018.02
7.0.40.0070.08317.91
7.0.30.0070.06317.93
7.0.20.0100.08017.98
7.0.10.0030.04317.99
7.0.00.0070.07318.01
5.6.280.0070.07021.21
5.6.210.0030.04020.88
5.6.200.0070.05318.37
5.6.190.0130.04318.32
5.6.180.0130.04718.34
5.6.170.0030.06018.45
5.6.160.0100.06718.37
5.6.150.0070.05318.43
5.6.140.0100.03718.36
5.6.130.0030.08018.34
5.6.120.0100.04718.37
5.6.110.0030.08318.39
5.6.100.0130.07318.41
5.6.90.0000.08718.33
5.6.80.0030.06317.75
5.6.70.0030.07317.75
5.6.60.0030.04017.71
5.6.50.0100.04717.71
5.6.40.0030.07017.79
5.6.30.0100.04317.78
5.6.20.0030.05017.64
5.6.10.0030.08017.77
5.6.00.0030.05717.71
5.5.350.0030.05320.65
5.5.340.0070.04018.19
5.5.330.0100.07718.09
5.5.320.0100.04018.13
5.5.310.0100.05018.21
5.5.300.0030.09018.17
5.5.290.0070.08318.21
5.5.280.0030.08718.11
5.5.270.0100.06018.22
5.5.260.0000.05718.16
5.5.250.0100.07718.05
5.5.240.0130.07017.62
5.5.230.0030.04317.46
5.5.220.0070.03717.48
5.5.210.0100.03717.52
5.5.200.0070.05017.50
5.5.190.0070.07717.52
5.5.180.0000.07717.46
5.5.160.0030.07717.55
5.5.150.0100.07717.55
5.5.140.0070.05317.54
5.5.130.0170.06017.48
5.5.120.0100.05717.43
5.5.110.0070.05717.51
5.5.100.0130.04717.48
5.5.90.0070.04017.34
5.5.80.0100.06317.44
5.5.70.0030.06017.44
5.5.60.0030.08017.41
5.5.50.0070.07317.36
5.5.40.0070.05017.38
5.5.30.0070.08017.41
5.5.20.0100.07017.38
5.5.10.0130.07317.38
5.5.00.0130.09317.32
5.4.450.0070.06019.43
5.4.440.0100.09019.67
5.4.430.0030.07019.48
5.4.420.0130.07319.52
5.4.410.0100.07719.45
5.4.400.0100.06719.30
5.4.390.0070.04719.25
5.4.380.0030.05319.16
5.4.370.0000.04319.30
5.4.360.0170.06719.31
5.4.350.0100.06719.20
5.4.340.0170.06019.25
5.4.320.0000.08019.28
5.4.310.0000.08319.41
5.4.300.0070.07019.30
5.4.290.0100.04319.19
5.4.280.0100.07019.28
5.4.270.0000.08019.16
5.4.260.0130.06319.16
5.4.250.0230.06319.24
5.4.240.0170.03319.20
5.4.230.0030.08319.26
5.4.220.0070.07719.26
5.4.210.0100.07719.13
5.4.200.0070.03719.13
5.4.190.0070.08019.27
5.4.180.0170.03719.41
5.4.170.0130.06719.11
5.4.160.0030.07019.02
5.4.150.0070.04319.30
5.4.140.0030.04716.63
5.4.130.0070.04716.62
5.4.120.0030.04316.58
5.4.110.0100.07316.68
5.4.100.0030.05716.56
5.4.90.0070.04716.67
5.4.80.0170.06316.64
5.4.70.0100.03716.57
5.4.60.0070.04016.55
5.4.50.0030.03716.59
5.4.40.0030.07016.58
5.4.30.0070.03316.59
5.4.20.0070.08016.48
5.4.10.0000.07016.55
5.4.00.0100.04016.11
5.3.290.0070.06714.86
5.3.280.0000.08014.84
5.3.270.0030.08015.00
5.3.260.0070.04314.86
5.3.250.0070.04314.84
5.3.240.0070.07314.99
5.3.230.0100.07314.84
5.3.220.0000.04314.76
5.3.210.0070.04014.78
5.3.200.0000.04314.79
5.3.190.0000.07714.96
5.3.180.0100.06714.76
5.3.170.0070.05714.77
5.3.160.0100.04714.95
5.3.150.0130.05314.80
5.3.140.0130.07314.85
5.3.130.0100.07314.76
5.3.120.0030.05014.57
5.3.110.0070.07714.73
5.3.100.0030.07714.22
5.3.90.0000.05314.26
5.3.80.0070.07714.18
5.3.70.0100.07314.25
5.3.60.0070.04714.23
5.3.50.0030.04014.18
5.3.40.0000.04714.18
5.3.30.0130.02714.09
5.3.20.0030.06013.94
5.3.10.0030.05313.85
5.3.00.0030.04313.82
5.2.170.0000.04311.12
5.2.160.0030.03011.11
5.2.150.0070.02711.13
5.2.140.0070.02710.92
5.2.130.0030.06011.07
5.2.120.0030.05311.08
5.2.110.0070.05311.02
5.2.100.0100.06011.07
5.2.90.0070.03711.09
5.2.80.0070.06310.87
5.2.70.0170.05011.06
5.2.60.0000.06011.05
5.2.50.0030.06311.02
5.2.40.0000.03710.96
5.2.30.0070.06010.91
5.2.20.0100.05710.95
5.2.10.0070.05310.88
5.2.00.0030.05010.75
5.1.60.0130.0479.91
5.1.50.0030.0539.98
5.1.40.0030.0539.92
5.1.30.0070.05310.29
5.1.20.0000.04010.27
5.1.10.0070.05310.06
5.1.00.0070.05310.10
5.0.50.0070.0478.54
5.0.40.0030.0208.23
5.0.30.0030.0478.23
5.0.20.0030.0378.26
5.0.10.0000.0338.37
5.0.00.0030.0338.09
4.4.90.0000.0277.98
4.4.80.0000.0377.98
4.4.70.0100.0307.98
4.4.60.0030.0177.98
4.4.50.0070.0107.98
4.4.40.0030.0577.98
4.4.30.0030.0207.98
4.4.20.0000.0207.98
4.4.10.0030.0337.98
4.4.00.0030.0437.98
4.3.110.0000.0407.98
4.3.100.0130.0177.98
4.3.90.0000.0337.98
4.3.80.0030.0237.98
4.3.70.0000.0337.98
4.3.60.0000.0377.98
4.3.50.0000.0337.98
4.3.40.0000.0537.98
4.3.30.0000.0407.98
4.3.20.0000.0307.98
4.3.10.0000.0377.98
4.3.00.0000.0277.98

preferences:
43.52 ms | 400 KiB | 5 Q