3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @param string $T the string to re-order * @param string $O the string that specifies the desired order * @return string the re-ordered $T */ function reorder($T, $O) { // Convert $T and $O to arrays of characters $t = str_split($T); $o = str_split($O); // Compute the index of each character from $T in $O // Use -1 for characters that do not appear in $O $m = array_map(function($x) use ($o) { $p = array_search($x, $o); return ($p === FALSE) ? -1 : $p; }, $t ); // Sort $m and $t simultaneously; $t will follow the changes applied to $m array_multisort($m, $t); // Join the sorted characters of $T back and return as string return implode('', $t); } $order = 'eloh'; echo('hello => '.reorder('hello', $order)."\n"); echo('help => '.reorder('help', $order)."\n");
Output for git.master, git.master_jit, rfc.property-hooks
hello => elloh help => pelh

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
49.53 ms | 401 KiB | 8 Q