Q: replace word with another word - search & replace are red into an array This is a follow-up question to one I posed earlier:
I try to replace a person's married title in a genealogy-program.
Like: "replace title in the lastname-string at the end with the female version".
The title is $mpref. In the csv are the male titles (to lookup) and the female  titles (to replace with):
            $mpref = trim($mpref);
            $file = fopen("mods/m_replace.csv","r");

            while (($csv = fgetcsv($file)) !== false) {
                $search = array();
                $replace= array();
                $search = $csv[0];
                $replace = $csv[1];
            }
            fclose($file);
            $blub = str_replace($search, $replace, $mpref);
            $lastname = "{$blub} {$mName} ({$text['nee']} {$lastname})";


It works...partially. However, I still have a problem:
It replaces the title only if original_title and replacement_title are at [0] and [1] in the csv - not if the pair is [2] and [3], or [4] and [5]...despite iterating via "while"
e.g. from csv:
Herzog, Herzogin
Freiherr, Freiherrin
Graf, Gräfin


...Leading to something like "Marie Louise Freiherr von Hardtenstein (nee Becker)" instead of "Marie Louise Freiherrin von Hardtenstein (nee Becker)"...

A: This is because you need to move your $search and $replace array initialization outside the while loop.
            $mpref = trim($mpref);
            $file = fopen("mods/m_replace.csv","r");
            $search = array();  //Define before the loop
            $replace = array(); //Define before the loop

            while (($csv = fgetcsv($file)) !== false) {
                //$search = array();    //Commented this as it should be outside the loop.
                //$replace= array();    //Commented this as it should be outside the loop.
                $search[] = $csv[0];    //Add Value to Array. See the []
                $replace[] = $csv[1];   //Add Value to Array. See the []
            }
            fclose($file);
            $blub = str_replace($search, $replace, $mpref);
            $lastname = "{$blub} {$mName} ({$text['nee']} {$lastname})";

