<?php
$patterns = ['/\bhim\b/', '/\bhimself\b/', '/\bhe\b/', '/\bhis\b/'];
$replacements = ['her', 'herself', 'she', 'her'];
$str = 'he went by "himself" (his imaginary friend came with him) to the himalayas. this was not the first time in history ';
echo preg_replace($patterns, $replacements, $str);
she went by "herself" (her imaginary friend came with her) to the himalayas. this was not the first time in history
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
she went by "herself" (her imaginary friend came with her) to the himalayas. this was not the first time in history