3v4l.org

run code in 300+ PHP versions simultaneously
<?php function replace($row){ $replaced = preg_replace_callback("~(\<span class=\"itemopener\"\>)(\d{2})\s(top\</span\>.*\<span class=\"allopener\"\>).{3}(\</span\>)~iU", function($matches){ $str = $matches[1] . $matches[3] . $matches[2] . $matches[4]; return $str; }, $row); return $replaced; } $s = '<span class="itemopener">82 top</span>&nbsp;<span class="allopener">all</span>'; $replaced = replace($s); echo "<pre>" . print_r($replaced, 1) . "</pre>"; exit();

preferences:
29.37 ms | 405 KiB | 5 Q