I get values from the textarea
as follows:
2342
de20343
23094
zz900
234432
zz900
2342
I want to find duplicates (eg: "zz900" and "2342") and delete them. In addition, blank lines must be removed.
Is there a single function that will do this?
I get values from the textarea
as follows:
2342
de20343
23094
zz900
234432
zz900
2342
I want to find duplicates (eg: "zz900" and "2342") and delete them. In addition, blank lines must be removed.
Is there a single function that will do this?
Have you tried exploding the text by a newline, and use array_unique to remove duplicates and use array_filter to remove empty lines?
Something like:
<?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);
?>
This should work for you:
$parts = array_values(array_unique(array_filter(explode(PHP_EOL, $str))));
print_r($parts);
As an example:
$str = "
2342
de20343
23094
zz900
234432
zz900
2342";
$parts = array_values(array_unique(array_filter(explode(PHP_EOL, $str))));
print_r($parts);
Output:
Array ( [0] => 2342 [1] => de20343 [2] => 23094 [3] => zz900 [4] => 234432 )
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;
?>