Php 用另一个单词替换单词-搜索&;将红色替换为一个数组
这是我先前提出的一个后续问题: 我试图在家谱项目中取代一个人的已婚头衔。 例如:“将末尾lastname字符串中的标题替换为女性版本”。 标题是$mpref。csv中有男性标题(查找)和女性标题(替换为): 它部分地起作用。但是,我仍然有一个问题: 仅当原始标题和替换标题在csv中位于[0]和[1]时,它才替换标题-如果该对为[2]和[3]或[4]和[5]…则不替换,尽管通过“while”进行迭代Php 用另一个单词替换单词-搜索&;将红色替换为一个数组,php,arrays,csv,replace,while-loop,Php,Arrays,Csv,Replace,While Loop,这是我先前提出的一个后续问题: 我试图在家谱项目中取代一个人的已婚头衔。 例如:“将末尾lastname字符串中的标题替换为女性版本”。 标题是$mpref。csv中有男性标题(查找)和女性标题(替换为): 它部分地起作用。但是,我仍然有一个问题: 仅当原始标题和替换标题在csv中位于[0]和[1]时,它才替换标题-如果该对为[2]和[3]或[4]和[5]…则不替换,尽管通过“while”进行迭代 …导致类似“Marie Louise Freiherr von Hardtenstein(nee
…导致类似“Marie Louise Freiherr von Hardtenstein(nee Becker)”而不是“Marie Louise Freiherr von Hardtenstein(nee Becker)”…这是因为需要将
$search
和$replace
数组初始化移到循环之外
$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})";
这是因为您需要将$search
和$replace
数组初始化移到while
循环之外
$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})";
我们怎么知道你之前发布了什么?我们怎么知道你之前发布了什么?你好,谢谢。是的,这是一个错误——但不幸的是,这不是解决问题的方法。还有哪些问题可能是原因(…)?@Tontaube请查看最新编辑<代码>$search
循环内部应该是$search[]
。美元也一样,明白了。。。我把整段插入了一个错误的位置,因此它被覆盖了…你好,谢谢。是的,这是一个错误——但不幸的是,这不是解决问题的方法。还有哪些问题可能是原因(…)?@Tontaube请查看最新编辑<代码>$search循环内部应该是$search[]
。美元也一样,明白了。。。我把整段插入了一个错误的位置,因此它被覆盖了。。。
$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})";