3v4l.org

run code in 300+ PHP versions simultaneously
<?php function replace2($matches, $replacements, $frame) { if (!isset($matches[0]) || !is_array($matches[0]) || empty($matches[0])) { return []; } $result = []; foreach ($matches[0] as $value) { if (isset($replacements[$value])) { foreach ($replacements[$value] as $v) { $tmp = str_replace($value, $v, $frame); $result[$tmp] = $tmp; array_shift($matches); replace2($matches, $replacements, $frame); } } } return $result; } function doStuff($frame) { $replacements = [ '{childName}' => [], '{favoriteFood}' => ['pizza', 'soup', 'burger'], '{favoriteToy}' => ['doll', 'teddy bear', 'truck', 'computer'] ]; preg_match_all('/'.implode('|', array_keys($replacements)).'/', $frame, $matches); if (count($matches[0]) < 1) { return [$frame => null]; } return replace2($matches, $replacements, $frame); } $frames = [ '{childName} loves to eat {favoriteFood} and play with his or her {favoriteToy}', '{childName} loves to eat {favoriteFood}', '{childName} loves to play' ]; foreach ($frames as $frame) { var_dump(doStuff($frame)); echo "\n"; }
Output for git.master, git.master_jit, rfc.property-hooks
array(7) { ["{childName} loves to eat pizza and play with his or her {favoriteToy}"]=> string(69) "{childName} loves to eat pizza and play with his or her {favoriteToy}" ["{childName} loves to eat soup and play with his or her {favoriteToy}"]=> string(68) "{childName} loves to eat soup and play with his or her {favoriteToy}" ["{childName} loves to eat burger and play with his or her {favoriteToy}"]=> string(70) "{childName} loves to eat burger and play with his or her {favoriteToy}" ["{childName} loves to eat {favoriteFood} and play with his or her doll"]=> string(69) "{childName} loves to eat {favoriteFood} and play with his or her doll" ["{childName} loves to eat {favoriteFood} and play with his or her teddy bear"]=> string(75) "{childName} loves to eat {favoriteFood} and play with his or her teddy bear" ["{childName} loves to eat {favoriteFood} and play with his or her truck"]=> string(70) "{childName} loves to eat {favoriteFood} and play with his or her truck" ["{childName} loves to eat {favoriteFood} and play with his or her computer"]=> string(73) "{childName} loves to eat {favoriteFood} and play with his or her computer" } array(3) { ["{childName} loves to eat pizza"]=> string(30) "{childName} loves to eat pizza" ["{childName} loves to eat soup"]=> string(29) "{childName} loves to eat soup" ["{childName} loves to eat burger"]=> string(31) "{childName} loves to eat burger" } array(0) { }

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:
57.51 ms | 404 KiB | 8 Q