我是初学者。我想读取一个 CSV,如果一个字符串包含一些单词 - 这将是 CSV 数组中的 (position)[x] - 我想用单词 B (position[x+1]) 替换它。我已经阅读了很多可能包含答案的帖子,但我无法让它发挥作用。这是我到目前为止所拥有的:$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条经验 获得超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 关注
- 223 浏览
添加回答
举报
0/150
提交
取消
