@ 2014-01-13T00:19:31Z <?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));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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).
Version System time (s) User time (s) Memory (MiB) 5.5.8 0.007 0.053 19.48 5.5.7 0.003 0.040 19.23 5.5.6 0.013 0.033 19.44 5.5.5 0.013 0.073 19.44 5.5.4 0.007 0.050 19.30 5.5.3 0.003 0.040 19.32 5.5.2 0.007 0.073 19.43 5.5.1 0.000 0.057 19.43 5.5.0 0.007 0.067 19.42 5.4.24 0.007 0.037 18.86 5.4.23 0.003 0.057 19.07 5.4.22 0.010 0.043 19.23 5.4.21 0.003 0.037 19.14 5.4.20 0.007 0.037 18.84 5.4.19 0.003 0.050 19.15 5.4.18 0.007 0.050 19.17 5.4.17 0.003 0.067 19.23 5.4.16 0.003 0.070 19.18 5.4.15 0.003 0.067 19.24 5.4.14 0.010 0.060 16.54 5.4.13 0.003 0.077 16.40 5.4.12 0.010 0.060 16.59 5.4.11 0.007 0.053 16.46 5.4.10 0.003 0.053 16.48 5.4.9 0.007 0.060 16.34 5.4.8 0.003 0.043 16.48 5.4.7 0.003 0.053 16.45 5.4.6 0.010 0.070 16.39 5.4.5 0.003 0.077 16.36 5.4.4 0.017 0.060 16.41 5.4.3 0.007 0.060 16.43 5.4.2 0.007 0.050 16.43 5.4.1 0.010 0.060 16.39 5.4.0 0.010 0.033 15.74 5.3.28 0.007 0.080 14.78 5.3.27 0.013 0.077 14.55 5.3.26 0.007 0.037 14.77 5.3.25 0.010 0.063 14.65 5.3.24 0.013 0.073 14.55 5.3.23 0.013 0.067 14.62 5.3.22 0.003 0.080 14.60 5.3.21 0.010 0.073 14.62 5.3.20 0.010 0.073 14.62 5.3.19 0.003 0.077 14.75 5.3.18 0.000 0.043 14.62 5.3.17 0.010 0.067 14.60 5.3.16 0.010 0.063 14.61 5.3.15 0.003 0.080 14.63 5.3.14 0.003 0.077 14.62 5.3.13 0.017 0.063 14.56 5.3.12 0.003 0.050 14.56 5.3.11 0.000 0.043 14.48 5.3.10 0.017 0.040 14.11 5.3.9 0.007 0.033 14.09 5.3.8 0.003 0.040 14.08 5.3.7 0.010 0.070 14.11 5.3.6 0.010 0.070 14.00 5.3.5 0.003 0.070 13.92 5.3.4 0.010 0.033 14.12 5.3.3 0.000 0.040 14.08 5.3.2 0.007 0.080 13.76 5.3.1 0.007 0.067 13.75 5.3.0 0.013 0.063 13.66
preferences:dark mode live preview
144.58 ms | 1386 KiB | 7 Q