簡體   English   中英

PHP:如何用字符串中的單詞 B 替換單詞 A - A 和 B 位於數組 [] 和 [+1] 中

[英]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);

strtr手冊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM