[英]PHP: How to replace word A with word B in a string - A and B being in an array [] and [+1]
我是初學者。
我想讀取一個 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);
$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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.