Please [edit] and provide mcve: some specific values of
$search_string, $replace_string and $filePath for which the problem occurs.