2010-02-15 112 views
5

家伙,我有一个文本文件,我想删除包含特定的词的某些行删除行包含特定的词/词组用PHP

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

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

现在有阵从中我想删除一些线路并想使用它们等等。

回答

7

当你在一个阵列的行文件的每一行,该array_filter功能你可能感兴趣的(引用)

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

迭代所输入的每个值 将数组传递给回调函数 。
如果回调函数 返回true,则输入中的当前值 将返回到 结果数组中。阵列密钥保留 。

你也可以使用strposstripos,以确定是否一个字符串包含在一个又一个。


举例来说,假设我们有这个数组:

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

我们可以定义一个回调函数,将筛选出含“glop”线:

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

,并使用该功能与array_filter

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

而且我们会得到这样的输出:

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

即我们已经删除含有“BADWORD”“GLOP”的所有行。


当然,现在你有基本的想法,没有什么能阻止你使用更复杂的回调函数;-)意见后


编辑:这里的代码的完整部分,其应工作:

首先,你有你的行列表:

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

然后,您从文件加载坏词的列表:
然后您修剪每一行,并删除空行,以确保您最终只能得到$bad_words数组中的“单词”,而不是空白的东西,这会导致烦恼。

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

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

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

然后,回调函数,即遍历数组的脏话:
注:使用全局变量不说nice :-(但array_filter调用的回调函数没有得到任何其他参数,并且我不希望在每次调用回调函数时加载文件。

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

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

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

其中,这个时候,为您提供:

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

希望这有助于。

+0

非常感谢解释整个 – Jimmy

+1

不客气:-)玩得开心! –

+0

只是告诉我如果我想添加一个单词列表,如果我可以写另一个单词列表,每行一个单词,然后做我想做的事情?如果我想用glop添加更多单词 – Jimmy

2

查看strpos功能。它可以告诉你一个字符串是否包含另一个字符串(以及第一个字符串在第二个字符串中的位置)。你会使用这样的:

$good = array(); 
$bad_words = array('martin', 'methew'); 

// for every line in the file 
foreach($raw as $line) { 
    // check for each word we want to avoid 
    foreach($bad_words as $word) { 
    // if this line has a trigger word 
    if(strpos($line, $word) !== false) { 
     // skip it and start processing the next 
     continue 2; 
    } 
    } 

    // no triggers hit, line is clean 
    $good[] = $line; 
} 

现在,你将不得不$good只有线条简洁的列表。

+0

只是告诉我,如果我想做另一个坏字,我会做。txt文件,并用它与此解决方案,我试过但失败,其实我不想再次打开代码n再次:D感谢您的帮助 – Jimmy

0
<?php 
$source = "problem.txt"; 
$raw = file_get_contents($source) or die("Cannot read file"); 
$wordlist = "martin|methew|asshole"; 
$raw = preg_replace("/($wordlist)/ie", "", $raw); 
file_put_contents($source, $raw); 
?> 
+0

哇惊人的帮助迅速,现在我太高兴和困惑:D 以及确定东西gona工作,非常感谢 上帝保佑你 – Jimmy

2

这将删除所有有上黑名单的词吧:

$rows = file("problem.txt");  
$blacklist = "foo|bar|lol"; 

foreach($rows as $key => $row) { 
    if(preg_match("/($blacklist)/", $row)) { 
     unset($rows[$key]); 
    } 
} 

file_put_contents("solved.txt", implode("\n", $rows)); 

或者,如果你正在使用PHP 5.3,你可以使用lambda函数array_filter:

$rows = file("problem.txt");  
$blacklist = "foo|bar|lol"; 
$rows = array_filter($rows, function($row) { 
    return preg_match("/($blacklist)/", $row); 
}); 

file_put_contents("solved.txt", implode("\n", $rows)); 

在PHP 5.3之前,使用array_filter的解决方案实际上会占用比我发布的第一个解决方案更多的行,所以我会放弃这一点。

1

假设你拥有的“脏话”的数组:

<?php 
foreach ($raw as $key=>$line) 
{ 
    foreach ($badwords as $w) 
    { 
     if (strpos($line, $w) !== false) 
      unset($raw[$key]); 
    } 
} 
?> 
2
$file=file("problem.txt"); 
$a = preg_grep("/martin|john/",$file,PREG_GREP_INVERT); 
print_r($a); 
2

如果你有很长的字符串,而不是一个文件,要删除具有特定单词的所有串线。你可以使用这个:

$string="I have a long string\n 
    That has good words inside.\n 
    I love my string.\n 
    //add some words here\n"; 
$rows = explode("\n",$string); 
$unwanted = "tring|\/\/"; 
$cleanArray= preg_grep("/$unwanted/i",$rows,PREG_GREP_INVERT); 
$cleanString=implode("\n",$cleanArray); 
print_r ($cleanString); 

这将删除包含“tring”和“//”的行。

相关问题