Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用另一个文件替换文件中的单词_Php_Str Replace - Fatal编程技术网

在PHP中使用另一个文件替换文件中的单词

在PHP中使用另一个文件替换文件中的单词,php,str-replace,Php,Str Replace,我有一个文件,上面有一段文字(Assignment2inputfile.txt)。我可以很好地打开那个文件。我有另一个文件(stopwords),其中有一个单词列表,如果在Assignment2inputfile中找到,需要用单词“stop”替换(我将它放在代码中的所有大写字母中,以便在它工作时立即看到)。我觉得我正处在我所需要的边缘,但是替代者并没有发生。这是一个练习,这就是为什么我的变量的命名非常笼统,或者与它们正在做的事情有关(chng->change->更改原始文件;$new->更改的结

我有一个文件,上面有一段文字(Assignment2inputfile.txt)。我可以很好地打开那个文件。我有另一个文件(stopwords),其中有一个单词列表,如果在Assignment2inputfile中找到,需要用单词“stop”替换(我将它放在代码中的所有大写字母中,以便在它工作时立即看到)。我觉得我正处在我所需要的边缘,但是替代者并没有发生。这是一个练习,这就是为什么我的变量的命名非常笼统,或者与它们正在做的事情有关(chng->change->更改原始文件;$new->更改的结果)


str_replace
可以将字符串数组作为其第一个参数,它将在目标字符串中查找并替换每个字符串。所以这里

$chng = str_replace("stopwords", 'STOP', $x);
“stopwords”
需要是一个数组
$stopwords
,其中包含该文件中的单词列表

获取该数组最简单的方法可能是使用
file
,这是一个将文件读入数组的函数

$stopwords = file('stopwords.txt', FILE_IGNORE_NEW_LINES);
$chng = str_replace($stopwords, 'STOP', $x);
FILE\u IGNORE\u NEW\u需要行
,否则数组中的字符串将包含新行,因此可能与其他文件中的任何内容都不匹配



有点像旁注,但是
file\u put\u contents
不返回新内容。因此,如果您想查看修改后的文本,只需
echo$chng
而不是
$new

在这里,我将为您提供可靠的(未经测试的)

基本上,在过滤停止字(数组)之后,您会得到如下模式

/\b(the|and|for)\b/
模式基本上是

  • \b
    单词边界
  • (…|…)
    是或
但是您需要修剪和预引用它们,这就是数组映射所做的

如果你只是用“停止”来代替所有的单词,这应该没问题


哦和
'stopwords.txt'
应该是stopwords文件的名称。

stopwords文件的结构是什么?它只是一个单词列表,每一个单词都在一行,或者CSV,或者其他什么?看起来您并没有在代码中加载该文件。(第2行也有语法错误,但这可能只是你问题中的一个输入错误。)一个真实的例子会非常有用。例如,如果您想替换
red
而不是
redemption
,更好的用法是
preg\u replace
和单词边界。完成赋值缺少的关键点是,您需要有一个数组,在
str\u replace
函数中包含字符串“stopwords”。如何获得该数组取决于stopwords文件的结构。当放到服务器上并在浏览器中访问时,stopwords文件是一个庞大的单词列表,每行一个,按字母顺序排列。是的,我刚刚修复了第2行的错误,它是一个打字错误。您是使用
文件
还是
文件获取内容
$x = file_get_contents('Assignment2inputfile.txt');

//if file returns false we cant use a boolean as an array, so this is more sensable
if(false === ($stopwords = file('stopwords.txt', FILE_SKIP_EMPTY_LINES))) throw new Exception('Could not load stop words from file');

$stopwords = array_map(function($item){
    return preg_quote(trim($item),'/');
}, $product);
$pattern = '/\b('.implode('|', $stopwords).')\b/';

$chng = preg_replace($pattern, 'STOP', $x); 
$new = file_put_contents('Assignment2inputfile.txt', $chng);
/\b(the|and|for)\b/