3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = 'gamer thing gamer games test games'; $pattern = '/\b(?<keyword>gamer|games)\b/'; // We'll append to this array after we use a given keyword $usedKeywords = []; $finalString = preg_replace_callback( $pattern, // Remember to capture the array by-ref static function (array $matches) use (&$usedKeywords) { $thisKeyword = $matches['keyword']; if (in_array($thisKeyword, $usedKeywords, true)) { return $thisKeyword; } $usedKeywords[] = $thisKeyword; // Do your replacement here return '~'.$thisKeyword.'~'; }, $string ); print_r($finalString);
Output for git.master, git.master_jit, rfc.property-hooks
~gamer~ thing gamer ~games~ test games

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:
154.49 ms | 405 KiB | 5 Q