3v4l.org

run code in 300+ PHP versions simultaneously
<?php function translatePronouns($text) { static $replacePairs = [ 'iyou' => ['i', 'you'], 'youi' => ['^you', 'i'], 'myyour' => ['my', 'your'], 'yourmy' => ['your', 'my'], 'meyou' => ['me', 'you'], 'youme' => ['you', 'me'], 'myselfyourself' => ['myself', 'yourself'], 'yourselfmyself' => ['yourself', 'myself'], 'mineyours' => ['mine', 'yours'], 'yoursmine' => ['yours', 'mine'], ]; static $expr; if (!isset($expr)) { $parts = []; foreach ($replacePairs as $name => $pair) { $parts[] = "(?<=^|\s)(?P<$name>$pair[0])(?=$|\s)"; } $expr = '#' . implode('|', $parts) . '#i'; } return preg_replace_callback($expr, function($match) use($replacePairs) { foreach ($match as $name => $text) { if ($text !== '' && isset($replacePairs[$name])) { return $replacePairs[$name][1]; } } // var_dump($match); //return $replacePairs[$match[0]]; }, $text); } $strings = [ 'i should go fuck myself', 'i will kill you if i decide it suits me to do so', 'you will eat me', 'my boobs are bigger than yours' ]; foreach ($strings as $string) { echo translatePronouns($string) . "\n"; }

preferences:
26.55 ms | 405 KiB | 5 Q