- preg_replace: documentation ( source)
<?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).';
echo preg_replace($patterns, $replacements, $str);