Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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函数构造的数组中的文本_Php_Arrays_Regex - Fatal编程技术网

PHP——替换由文件PHP函数构造的数组中的文本

PHP——替换由文件PHP函数构造的数组中的文本,php,arrays,regex,Php,Arrays,Regex,我有字符串,我想删除字符串和数组中的单词。该数组是使用file函数和preg_replace函数创建的,用于从数组中删除句子中出现的模式 虽然我没有收到错误,但替换不起作用。我真的很感激任何帮助,我在过去的三天里一直在努力让它工作,但我没有做到这一点:(这让我发疯 这就是我到目前为止所做的: PHP代码: $test=file('files/stop_words.txt'); echo $test; $no_stop=str_replace('|$test|', 'lllll', $sente

我有字符串,我想删除字符串和数组中的单词。该数组是使用file函数和preg_replace函数创建的,用于从数组中删除句子中出现的模式

虽然我没有收到错误,但替换不起作用。我真的很感激任何帮助,我在过去的三天里一直在努力让它工作,但我没有做到这一点:(这让我发疯

这就是我到目前为止所做的:

PHP代码:

$test=file('files/stop_words.txt');
echo $test;

$no_stop=str_replace('|$test|', 'lllll', $sentence);
echo "<br>" .$no_stop;

谢谢

我正在添加一个示例句子,这样你就可以看到它的效果了

我用file|get|内容交换了文件,这样您可以在执行检查之前删除每侧的|

str|u replace可以接受数组,但必须为精确匹配进行设置,因此使用“|inter |”而不仅仅是“interne”也会造成阻碍

$sentence = "I stood alone in the yard";

$test = file_get_contents('files/stop_words.txt'); // Load in as a string

$test = str_replace("|", "", $test); // Remove unneeded |
$testList = explode("\n", $test); // Turn into an array

$filtered_sentence = str_replace($testList, 'lllll', $sentence); // Pass array into str_replace

echo $filtered_sentence;

谢谢Asperon,到目前为止,我还没有接近它。它很有效。
$sentence = "I stood alone in the yard";

$test = file_get_contents('files/stop_words.txt'); // Load in as a string

$test = str_replace("|", "", $test); // Remove unneeded |
$testList = explode("\n", $test); // Turn into an array

$filtered_sentence = str_replace($testList, 'lllll', $sentence); // Pass array into str_replace

echo $filtered_sentence;