我是初學(xué)者。我想讀取一個 CSV,如果一個字符串包含一些單詞 - 這將是 CSV 數(shù)組中的 (position)[x] - 我想用單詞 B (position[x+1]) 替換它。我已經(jīng)閱讀了很多可能包含答案的帖子,但我無法讓它發(fā)揮作用。這是我到目前為止所擁有的:$handle = fopen('xyz.csv')$csv = fgetcsv($handle);$input = "The fox ate the chicken";$blub = str_replace($csv[find_value], $csv[find_value + 1]$input);
1 回答

蕪湖不蕪
TA貢獻1796條經(jīng)驗 獲得超7個贊
$handle = fopen('xyz.csv', 'r'); // don't forget the mode
$replace = [];
// read file line by line and fill `$replace` array
while (($csv = fgetcsv($handle)) !== false) {
$replace[$csv[0]] = $csv[1];
}
$inputs = "The fox ate the chicken";
// perform replacement
$blub = strtr($inputs, $replace);
- 1 回答
- 0 關(guān)注
- 173 瀏覽
添加回答
舉報
0/150
提交
取消