0

我从textarea以下获得值:

2342
de20343
23094
zz900
234432
zz900
2342

我想找到重复项(例如:“zz900”和“2342”)并删除它们。此外,必须删除空白行。

是否有一个函数可以做到这一点?

4

3 回答 3

2

您是否尝试过用换行符分解文本,并使用 array_unique 删除重复项并使用 array_filter 删除空行?

就像是:

<?php
$text = '2342
de20343
23094
zz900
234432
zz900
2342';

$array = explode (PHP_EOL, $text);
$unique = array_filter (array_unique ($array));

echo join (PHP_EOL, $unique);
?>
于 2015-02-26T13:37:24.990 回答
1

这应该适合你:

$parts = array_values(array_unique(array_filter(explode(PHP_EOL, $str))));
print_r($parts);

举个例子:

$str = "
2342
de20343
23094


zz900
234432
zz900
2342";

$parts = array_values(array_unique(array_filter(explode(PHP_EOL, $str))));
print_r($parts);

输出:

Array ( [0] => 2342 [1] => de20343 [2] => 23094 [3] => zz900 [4] => 234432 )
于 2015-02-26T13:37:18.720 回答
0
Check this, this is new code:-

<?php
$string_old = "2342
de20343

_
_
23094
zz900
234432
zz900
2342";
$array_by_exploding_through_next_line = explode ("\n",$string_old);
$new_array = array();
foreach($array_by_exploding_through_next_line as $key => $value){
    if(strpos($array_by_exploding_through_next_line[$key], '_') !== FALSE || strpos($array_by_exploding_through_next_line[$key], ' ') !== FALSE){
    }else{
        $new_array[] = $value;
    }
}
$string_new = implode('',array_unique($array_by_exploding_through_next_line));
print_r($array_by_exploding_through_next_line);
echo "<br>";
echo $string_new;
die;
?>
于 2015-02-26T13:40:44.673 回答