使用 PHP 删除包含特定单词/短语的行 [英] Delete the line contains specific words/phrases with PHP

查看:23
本文介绍了使用 PHP 删除包含特定单词/短语的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我有一个文本文件,我想删除一些包含特定单词的行

 

现在有一个数组,我想从中删除一些行并想使用它们等等.

解决方案

由于文件的每一行都在一个数组的一行中,array_filter 函数可能会让你感兴趣(quoting) :

array array_filter ( array $input [, callback $callback ] )

<块引用>

迭代输入中的每个值将它们传递给回调的数组功能.
如果回调函数返回true,当前来自输入的值返回到结果数组.数组键是保存.

您可以使用 strposstripos 以确定一个字符串是否包含在另一个字符串中.

例如,假设我们有这个数组:

$arr = 数组('这是一个测试','glop 测试','我喜欢 php','一个坏词,glop 是',);

我们可以定义一个回调函数来过滤掉包含glop"的行:

function keep_no_glop($line) {如果 (strpos($line, 'glop') !== false) {返回假;}返回真;}

并将该函数与 array_filter 一起使用:

$arr_filtered = array_filter($arr, 'keep_no_glop');var_dump($arr_filtered);

我们会得到这样的输出:

数组0 =>字符串这是一个测试"(长度=14)2 =>字符串我喜欢 php"(长度=10)

即我们删除了所有包含badword"glop"的行.


当然,既然您已经有了基本的想法,那么您就可以使用更复杂的回调函数了 ;-)

<小时>

在评论后这里是应该工作的完整代码部分:

首先,你有你的行列表:

$arr = 数组('这是一个测试','glop 测试','我喜欢 php','一个坏词,glop 是',);

然后,您从文件中加载坏词列表:
然后修剪每一行,并删除空行,以确保在 $bad_words 数组中只出现单词",而不是会导致麻烦的空白内容.

$bad_words = array_filter(array_map('trim', file('your_file_with_bad_words.txt')));var_dump($bad_words);

$bad_words 数组包含,来自我的测试文件:

数组0 =>字符串glop"(长度=4)1 =>字符串测试"(长度=4)

然后是回调函数,它循环遍历该坏词数组:

注意:使用全局变量不是很好:-(但是array_filter调用的回调函数没有得到任何其他参数,我不想每次都加载文件回调函数被调用.

function keep_no_glop($line) {全球 $bad_words;foreach ($bad_words as $bad_word) {如果 (strpos($line, $bad_word) !== false) {返回假;}}返回真;}

而且,和以前一样,您可以使用 array_filter 来过滤行:

$arr_filtered = array_filter($arr, 'keep_no_glop');var_dump($arr_filtered);

这一次,给了你:

数组2 =>字符串我喜欢 php"(长度=10)

guys i have a text file and i want to remove some lines that contain specific words

 <?php
// set source file name and path
$source = "problem.txt";

// read raw text as array
$raw = file($source) or die("Cannot read file");

now there's array from which i want to remove some lines and want to use them so on.

解决方案

As you have each line of your file in a row of an array, the array_filter function might interest you (quoting) :

array array_filter  ( array $input  [, callback $callback  ] )

Iterates over each value in the input array passing them to the callback function.
If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

And you can use strpos or stripos to determine if a string is contained in another one.

For instance, let's suppose we have this array :

$arr = array(
  'this is a test',
  'glop test',
  'i like php',
  'a badword, glop is', 
);

We could define a callback function that would filter out lines containing "glop" :

function keep_no_glop($line) {
  if (strpos($line, 'glop') !== false) {
    return false;
  }
  return true;
}

And use that function with array_filter :

$arr_filtered = array_filter($arr, 'keep_no_glop');
var_dump($arr_filtered);

And we'd get this kind of output :

array
  0 => string 'this is a test' (length=14)
  2 => string 'i like php' (length=10)

i.e. we have removed all the lines containing the "badword" "glop".


Of course, now that you have the basic idea, nothing prevents you from using a more complex callback function ;-)


Edit after comments : here's a full portion of code that should work :

First of all, you have your list of lines :

$arr = array(
  'this is a test',
  'glop test',
  'i like php',
  'a badword, glop is', 
);

Then, you load the list of bad words from a file :
And you trim each line, and remove empty lines, to make sure you only end up with "words" in the $bad_words array, and not blank stuff that would cause troubles.

$bad_words = array_filter(array_map('trim', file('your_file_with_bad_words.txt')));
var_dump($bad_words);

The $bad_words array contains, from my test file :

array
  0 => string 'glop' (length=4)
  1 => string 'test' (length=4)

Then, the callback function, that loops over that array of bad words:

Note : using a global variable is not that nice :-( But the callback function called by array_filter doesn't get any other parameter, and I didn't want to load the file each time the callback function is called.

function keep_no_glop($line) {
  global $bad_words;
  foreach ($bad_words as $bad_word) {
      if (strpos($line, $bad_word) !== false) {
        return false;
      }
  }
  return true;
}

And, as before, you can use array_filter to filter the lines :

$arr_filtered = array_filter($arr, 'keep_no_glop');
var_dump($arr_filtered);

Which, this time, gives you :

array
  2 => string 'i like php' (length=10)

这篇关于使用 PHP 删除包含特定单词/短语的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆