STR_REPLACE 从 TXT 文件到字符串(使用逗号分隔符)

STR_REPLACE from TXT file to string (with Comma Separator)

我想使用 STR_REPLACE.

根据在 TXT 文件中创建的列表过滤字符串中的关键字(使用逗号分隔符)

如果您在 TXT 列表中找到该关键字,那么它将从字符串中删除。

$db_tag_new = str_replace('The,',"",$db_tag);

我的字符串:(每个单词都有一个逗号分隔符)

$db_tag="The,To,In,A,Of,Is,That,Will,And,Be,For,Rates,Or,Bank,0,It,More,Brexit,With,At,Has,Eu,Have,Said,Would,As,This,Britain,He,May,On,Pound,Are,Interest,Carney,By,They,But,Getty,First,Images,Vote,About,Leave,Even,Inflation,S,Up,Also,Negative";

thebadlist.txt: (每字换行)

The
To
In
A
Of
Is
That
Will
And

结果:

$db_tag="Rates,Bank,Brexit,Britain,Pound,Interest,Carney";

提前致谢

使用 file() 读取您的 list.txt,它将每一行读入一个数组。然后你可以使用 str_replace() 因为它允许 param1 和 param2 是数组,尽管你只需要为 parameter1

使用数组
<?php

$db_tag="The,To,In,A,Of,Is,That,Will,And,Be,For,Rates,Or,Bank,0,It,More,Brexit,With,At,Has,Eu,Have,Said,Would,As,This,Britain,He,May,On,Pound,Are,Interest,Carney,By,They,But,Getty,First,Images,Vote,About,Leave,Even,Inflation,S,Up,Also,Negative";

$filters = file('list.txt');
// add commas to the occurances
for ($i=0; $i<count($filters); $i++) {
    $filters[$i] = rtrim($filters[$i]) . ',';
}

$db_tag = str_replace($filters, '', $db_tag);

echo $db_tag.PHP_EOL;

list.txt 文件包含

The
To
In
A
Of
Is
That
Will
And
Or

结果:

Be,For,Rates,Bank,0,It,More,Brexit,With,At,Has,Eu,Have,Said,Would,As,This,Britain,He,May,On,Pound,Are,Interest,Carney,By,They,But,Getty,First,Images,Vote,About,Leave,Even,Inflation,S,Up,Also,Negative