3v4l.org

run code in 300+ PHP versions simultaneously
<?php $HTML = <<< HTML some data from <span class="positive">blahblah test</span> was not <span class="positive">statistically <span class="positive">valid</span></span> <span class="positive">not statistically valid</span> HTML; $listOfNegatives = ['not statistically valid', 'blahblah test']; /** * Extract all words and their corresponsing positions * @param [string] $HTML * @return [array] $HTMLWords */ function extractWords($HTML) { $HTMLWords = []; preg_match_all("~\b(?<![</])\w+\b(?![^<>]+>)~", $HTML, $words, PREG_OFFSET_CAPTURE); foreach ($words[0] as $word) { $HTMLWords[$word[1]] = $word[0]; } return $HTMLWords; } /** * Check if any of our defined list values can be found in an ordered-array of exctracted words * @param [array] $HTMLWords * @param [array] $listOfNegatives * @return [array] $subString */ function checkNegativesExistence($HTMLWords, $listOfNegatives) { $counter = 0; $previousWordOffset = null; $subStrings = []; foreach ($listOfNegatives as $i => $string) { $stringWords = explode(" ", $string); $wordIndex = 0; foreach ($HTMLWords as $offset => $HTMLWord) { if ($wordIndex > count($stringWords) - 1) { $wordIndex = 0; $counter++; } if ($stringWords[$wordIndex] == $HTMLWord) { $subStrings[$counter][] = [$HTMLWord, $offset, $previousWordOffset]; $wordIndex++; } elseif (isset($subStrings[$counter]) && count($subStrings[$counter]) > 0) { unset($subStrings[$counter]); $wordIndex = 0; } $previousWordOffset = $offset + strlen($HTMLWord); } $counter++; } return $subStrings; } /** * Substitute newly matched strings with negative HTML wrapper * @param [array] $subStrings * @param [string] $HTML * @return [string] $HTML */ function negativeHighlight($subStrings, $HTML) { $offset = 0; $HTMLLength = strlen($HTML); foreach ($subStrings as $key => $value) { $arrayOfWords = []; foreach ($value as $word) { $arrayOfWords[] = $word[0]; if (current($value) == $value[0]) { $start = substr($HTML, $word[1], strlen($word[0])) == $word[0] ? $word[2] : $word[2] + $offset; } if (current($value) == end($value)) { $defaultLength = $word[1] + strlen($word[0]) - $start; $length = substr($HTML, $word[1], strlen($word[0])) === $word[0] ? $defaultLength : $defaultLength + $offset; } } $string = implode(" ", $arrayOfWords); $HTML = substr_replace($HTML, "<span class=\"negative\">{$string}</span>", $start, $length); if ($HTMLLength > strlen($HTML)) { $offset = -($HTMLLength - strlen($HTML)); } elseif ($HTMLLength < strlen($HTML)) { $offset = strlen($HTML) - $HTMLLength; } } return $HTML; } $newHTML = negativeHighlight(checkNegativesExistence(extractWords($HTML), $listOfNegatives), $HTML); echo preg_replace_callback("~(<span[^>]+>([^<]*+<(?!/)(?:([a-zA-Z0-9]++)[^>]*>[^<]*</\3>|(?2)))*[^<]*</span>|(?'single'</[^<>]+>|<[^<>]+>))~", function ($match) { if (isset($match['single'])) { return null; } return $match[1]; }, $newHTML );

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.4.140.0130.00917.95
8.4.130.0150.00619.83
8.4.120.0150.00720.51
8.4.110.0100.01022.34
8.4.100.0110.00917.93
8.4.90.0150.00618.81
8.4.80.0040.00519.77
8.4.70.0050.00818.28
8.4.60.0170.00918.52
8.4.50.0110.00919.09
8.4.40.0070.01420.67
8.4.30.0060.01317.98
8.4.20.0070.01017.92
8.4.10.0000.00817.82
8.3.270.0110.01016.78
8.3.260.0150.00516.98
8.3.250.0030.00519.39
8.3.240.0130.00717.04
8.3.230.0120.00916.97
8.3.220.0140.00417.36
8.3.210.0030.00417.01
8.3.200.0070.00316.68
8.3.190.0030.00517.35
8.3.180.0100.01016.97
8.3.170.0100.01017.43
8.3.160.0030.01418.63
8.3.150.0040.00418.69
8.3.140.0060.00916.94
8.3.130.0040.00418.64
8.3.120.0040.00819.04
8.3.110.0050.00518.55
8.3.100.0030.00616.78
8.3.90.0080.00017.07
8.3.80.0060.00317.97
8.3.70.0070.00716.88
8.3.60.0130.01018.55
8.3.50.0130.00823.05
8.3.40.0110.00419.09
8.3.30.0130.00319.11
8.3.20.0040.00420.16
8.3.10.0030.00621.77
8.3.00.0030.00619.52
8.2.290.0070.00520.43
8.2.280.0090.00918.43
8.2.270.0150.00017.50
8.2.260.0040.01120.86
8.2.250.0150.00618.76
8.2.240.0030.00617.19
8.2.230.0000.00920.94
8.2.220.0000.00924.06
8.2.210.0040.00426.77
8.2.200.0060.00318.41
8.2.190.0110.00716.63
8.2.180.0220.00018.66
8.2.170.0100.01322.96
8.2.160.0110.00422.22
8.2.150.0040.00424.18
8.2.140.0080.00024.66
8.2.130.0000.00822.31
8.2.120.0000.00826.35
8.2.110.0100.00021.24
8.2.100.0060.00618.16
8.2.90.0050.00318.13
8.2.80.0000.00819.11
8.2.70.0050.00318.05
8.2.60.0080.00018.17
8.2.50.0050.00918.10
8.2.40.0040.00420.58
8.2.30.0040.00419.34
8.2.20.0000.00817.90
8.2.10.0000.00818.34
8.2.00.0030.00518.27
8.1.330.0120.00722.29
8.1.320.0130.00716.57
8.1.310.0090.00918.57
8.1.300.0100.00018.51
8.1.290.0090.00018.88
8.1.280.0110.00725.92
8.1.270.0000.00922.21
8.1.260.0040.00428.09
8.1.250.0050.00328.09
8.1.240.0050.00523.88
8.1.230.0080.00322.68
8.1.220.0060.00318.97
8.1.210.0030.00519.14
8.1.200.0030.00617.72
8.1.190.0040.00417.60
8.1.180.0040.00418.10
8.1.170.0060.00318.79
8.1.160.0040.00422.14
8.1.150.0040.00418.96
8.1.140.0080.00019.60
8.1.130.0040.00417.89
8.1.120.0050.00317.67
8.1.110.0040.00417.57
8.1.100.0040.00417.63
8.1.90.0000.00817.72
8.1.80.0050.00317.57
8.1.70.0050.00217.67
8.1.60.0000.00817.81
8.1.50.0030.00617.70
8.1.40.0090.00017.69
8.1.30.0030.00617.82
8.1.20.0000.00817.83
8.1.10.0040.00417.82
8.1.00.0040.00417.75
8.0.300.0040.00419.82
8.0.290.0000.00817.30
8.0.280.0060.00318.42
8.0.270.0040.00417.30
8.0.260.0030.00317.24
8.0.250.0030.00317.20
8.0.240.0050.00317.13
8.0.230.0050.00317.25
8.0.220.0040.00417.22
8.0.210.0080.00017.18
8.0.200.0000.00717.09
8.0.190.0040.00417.23
8.0.180.0030.00517.18
8.0.170.0000.00817.14
8.0.160.0080.00017.22
8.0.150.0000.01717.13
8.0.140.0000.00717.19
8.0.130.0030.00313.61
8.0.120.0040.00417.25
8.0.110.0000.00817.23
8.0.100.0070.00017.22
8.0.90.0040.00417.11
8.0.80.0030.01317.13
8.0.70.0040.00417.17
8.0.60.0030.00617.20
8.0.50.0040.00417.11
8.0.30.0100.00617.35
8.0.20.0130.00517.40
8.0.10.0050.00217.21
8.0.00.0130.00616.94
7.4.330.0000.00516.78
7.4.320.0070.00016.84
7.4.300.0040.00416.82
7.4.290.0040.00416.70
7.4.280.0070.00016.79
7.4.270.0050.00216.86
7.4.260.0050.00516.69
7.4.250.0040.00416.75
7.4.240.0040.00416.78
7.4.230.0070.00016.84
7.4.220.0030.02016.92
7.4.210.0090.00616.80
7.4.200.0090.00016.70
7.4.160.0100.00716.70
7.4.150.0080.01217.40
7.4.140.0090.00917.86
7.4.130.0120.00516.81
7.4.120.0090.01016.83
7.4.110.0080.01216.90
7.4.100.0130.00316.82
7.4.90.0060.01316.96
7.4.80.0160.00319.39
7.4.70.0120.00616.85
7.4.60.0130.00317.03
7.4.50.0060.01216.68
7.4.40.0070.01016.70
7.4.30.0080.00816.89
7.4.00.0060.00915.23
7.3.330.0030.00313.69
7.3.320.0000.00513.68
7.3.310.0070.00016.66
7.3.300.0030.00316.60
7.3.290.0090.00816.63
7.3.280.0100.00716.61
7.3.270.0170.00917.40
7.3.260.0110.00716.78
7.3.250.0090.01016.84
7.3.240.0060.01216.79
7.3.230.0130.00316.67
7.3.210.0070.01116.80
7.3.200.0070.01016.69
7.3.190.0030.01316.88
7.3.180.0030.01716.62
7.3.170.0030.01316.60
7.3.160.0070.01316.83
7.3.10.0120.00316.84
7.3.00.0060.00316.82
7.2.330.0030.01716.91
7.2.320.0120.00616.95
7.2.310.0080.00816.91
7.2.300.0170.00716.95
7.2.290.0130.00416.86
7.2.130.0000.01417.14
7.2.120.0100.00717.04
7.2.110.0030.01017.10
7.2.100.0070.00417.07
7.2.90.0090.00317.23
7.2.80.0100.00317.03
7.2.70.0030.00916.98
7.2.60.0030.01117.10
7.2.50.0070.01017.25
7.2.40.0070.01017.12
7.2.30.0130.00017.31
7.2.20.0000.01217.09
7.2.10.0060.00916.98
7.2.00.0090.00218.20
7.1.250.0040.00415.87
7.1.200.0040.00715.99
7.1.70.0050.00217.41
7.1.60.0070.00319.79
7.1.00.0030.08022.48
7.0.200.0430.00714.88
7.0.80.0070.05721.96
7.0.70.0130.06021.86
7.0.60.0070.05721.69
7.0.50.0130.05322.16
7.0.40.0130.05022.20
7.0.30.0070.06322.19
7.0.20.0030.06722.17
7.0.10.0100.05322.14
7.0.00.0100.05722.07
5.6.280.0030.06721.05
5.6.230.0000.06720.84
5.6.220.0100.05320.76
5.6.210.0170.04720.72
5.6.200.0070.06021.11
5.6.190.0130.05321.10
5.6.180.0030.06021.10
5.6.170.0200.05021.07
5.6.160.0070.05721.09
5.6.150.0130.05321.05
5.6.140.0100.05321.13
5.6.130.0030.06321.05
5.6.120.0070.06321.12
5.6.110.0130.05721.11
5.6.100.0030.06321.09
5.6.90.0070.05721.10
5.6.80.0130.04720.50
5.6.70.0070.05320.49
5.6.60.0070.05720.60
5.6.50.0100.05720.55
5.6.40.0100.05020.48
5.6.30.0170.05020.53
5.6.20.0100.06020.40
5.6.10.0030.05720.44
5.6.00.0100.05020.54
5.5.370.0070.05720.42
5.5.360.0030.06320.52
5.5.350.0000.06320.61
5.5.340.0030.06020.80
5.5.330.0100.05320.87
5.5.320.0130.05020.95
5.5.310.0170.05320.91
5.5.300.0100.05320.98
5.5.290.0030.06020.74
5.5.280.0100.05720.98
5.5.270.0130.05320.95
5.5.260.0130.05720.80
5.5.250.0130.05320.77
5.5.240.0200.05720.33
5.5.230.0130.05320.36
5.5.220.0100.06320.24
5.5.210.0070.05720.09
5.5.200.0070.06020.30
5.5.190.0030.06020.31
5.5.180.0100.05720.21
5.5.160.0030.05720.26
5.5.150.0030.05720.21
5.5.140.0130.04720.23
5.5.130.0170.06320.29
5.5.120.0170.05020.23
5.5.110.0070.05720.34
5.5.100.0070.06020.17
5.5.90.0070.05320.16
5.5.80.0100.05020.13
5.5.70.0070.05720.22
5.5.60.0030.05720.12
5.5.50.0000.05720.18
5.5.40.0100.07020.18
5.5.30.0070.05720.11
5.5.20.0130.05020.11
5.5.10.0030.05720.16
5.5.00.0070.05720.10

preferences:
35.82 ms | 403 KiB | 5 Q