3v4l.org

run code in 300+ PHP versions simultaneously
<?php $message = "This is a string using the terms? Wait here's one\nabukuma kai\nabukuma-kai\nabukuma\nabukuma\nkai abukuma abukuma kai"; $search = array( 'abukuma kai', 'abukuma' ); $replacements = array( '<a href="http://kancolle.wikia.com/wiki/Abukuma#Upgrade" class="screenshot" rel="ship/Abukuma_Kai.png" style="color:#0073e6; font-weight:bold; border-bottom: 1px #0073e6 dashed;"><p id="yosup">Abukuma Kai</p></a>', '<a href="http://kancolle.wikia.com/wiki/Abukuma#Basic" class="screenshot" rel="ship/Abukuma.png" style="color:#0073e6; font-weight:bold; border-bottom: 1px #0073e6 dashed;">Abukuma</a>' ); function replaceStrings($haystack, $needle, $replacement, $offset = 0) { if (count($needle) != count($replacement)) { throw new \InvalidArgumentException('Needle length must match replacement length'); } $regexpDelimiter = '/'; $wordDelimiter = '[- \t_]'; // Create the regexp if (is_array($needle)) { $massagedNeedles = array_map( function($s) use($regexpDelimiter, $wordDelimiter) { $s = preg_quote($s, $regexpDelimiter); $s = str_replace(' ', $wordDelimiter, $s); return $s; }, $needle ); // Sort it reverse so we get the longer strings first usort($massagedNeedles, function($a, $b) { return strlen($b) - strlen($a); }); $regexp = implode('|', $massagedNeedles); } else { $regexp = str_replace(' ', $wordDelimiter, preg_quote($needle, $regexpDelimiter)); } $regexp = sprintf('%1$s%2$s%1$si', $regexpDelimiter, $regexp); // Now find the next occurence using the regex we created if (!preg_match($regexp, $haystack, $matches, PREG_OFFSET_CAPTURE, $offset)) { // We've not found one. Let's say we're done. return $haystack; } // We've found one. Clean it up so we can find what replacement it uses $found = array_search(preg_replace('/' . $wordDelimiter . '/', ' ', $matches[0][0]), $needle); if ($found !== false) { $haystack = substr_replace($haystack, $replacement[$found], $matches[0][1], strlen($matches[0][0])); $offset = $matches[0][1] + strlen($replacement[$found]) } else { // This.. shouldn't happen but maybe it will. Let's code a bit defensively. $offset = $matches[0][1] + strlen($matches[0][0]); } return replaceStrings($haystack, $needle, $replacement, $offset); } var_dump(replaceStrings($message, $search, $replacements));

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)
5.5.80.0070.05319.48
5.5.70.0030.04019.23
5.5.60.0130.03319.44
5.5.50.0130.07319.44
5.5.40.0070.05019.30
5.5.30.0030.04019.32
5.5.20.0070.07319.43
5.5.10.0000.05719.43
5.5.00.0070.06719.42
5.4.240.0070.03718.86
5.4.230.0030.05719.07
5.4.220.0100.04319.23
5.4.210.0030.03719.14
5.4.200.0070.03718.84
5.4.190.0030.05019.15
5.4.180.0070.05019.17
5.4.170.0030.06719.23
5.4.160.0030.07019.18
5.4.150.0030.06719.24
5.4.140.0100.06016.54
5.4.130.0030.07716.40
5.4.120.0100.06016.59
5.4.110.0070.05316.46
5.4.100.0030.05316.48
5.4.90.0070.06016.34
5.4.80.0030.04316.48
5.4.70.0030.05316.45
5.4.60.0100.07016.39
5.4.50.0030.07716.36
5.4.40.0170.06016.41
5.4.30.0070.06016.43
5.4.20.0070.05016.43
5.4.10.0100.06016.39
5.4.00.0100.03315.74
5.3.280.0070.08014.78
5.3.270.0130.07714.55
5.3.260.0070.03714.77
5.3.250.0100.06314.65
5.3.240.0130.07314.55
5.3.230.0130.06714.62
5.3.220.0030.08014.60
5.3.210.0100.07314.62
5.3.200.0100.07314.62
5.3.190.0030.07714.75
5.3.180.0000.04314.62
5.3.170.0100.06714.60
5.3.160.0100.06314.61
5.3.150.0030.08014.63
5.3.140.0030.07714.62
5.3.130.0170.06314.56
5.3.120.0030.05014.56
5.3.110.0000.04314.48
5.3.100.0170.04014.11
5.3.90.0070.03314.09
5.3.80.0030.04014.08
5.3.70.0100.07014.11
5.3.60.0100.07014.00
5.3.50.0030.07013.92
5.3.40.0100.03314.12
5.3.30.0000.04014.08
5.3.20.0070.08013.76
5.3.10.0070.06713.75
5.3.00.0130.06313.66

preferences:
144.58 ms | 1386 KiB | 7 Q