PHP, 去除重复行和空行

PHP, Remove duplicate and blank line

我从 textarea 中获取值如下:

2342
de20343
23094
zz900
234432
zz900
2342

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

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

这应该适合你:

$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 )

您是否尝试过用换行符分解文本,并使用 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);
?>
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;
?>