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"; }
Output for 5.6.0 - 5.6.25, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.14
you should go fuck yourself you will kill me if you decide it suits you to do so i will eat you your boobs are bigger than mine

preferences:
176.36 ms | 408 KiB | 5 Q