Forum Moderators: coopster

Message Too Old, No Replies

array_unique is creating a NULL array?

I need to remove duplicates from an array, but array_unique is reaking havo

         

erikcw

7:47 pm on Jun 5, 2004 (gmt 0)

10+ Year Member



Hi All,

I'm writting a find and replace script for my sites internal search. Basically it is for the creation of a list of keywords that can later be fed through the search (kinda an advanced search of sorts)...

Anyways, I've rewritten the function several times, and I think I am getting close, but there is one last snag.

I am using array_unique() to remove the duplicates that are created by the function, and it is reutrning NULL. What's the deal?


function findreplace() {
extract($GLOBALS);

if ($hold == 1) { return trim(stripslashes($keywords)); }

$find = strtolower($_POST['find']);
$replace = strtolower($_POST['replace']);
$keywords = trim(strtolower($keywords));

//if ($_POST['replace_words'] == "Replace Words") {

$PATTERNS = "/\b" . $find . "\b/";
$REPLACEMENTS = $replace;
$keywords .= "\n" . preg_replace($PATTERNS, $REPLACEMENTS, $keywords);

$keywords_array = explode("\n", $keywords);

$Keywords_unique = array_unique($keywords_array);

$i = 0;
while($i < count($keywords_array)) {
$temp .= $keywords_array[$i] . "\n";
$i++;
}
$keywords = $temp;

// test if array is being created with matching entries so array_uniqye can proccess - YESS, it returns EQUAL
if ($keywords_array[1] === $keywords_array[4]) { $keywords = "EQUAL"; } else { $keywords = "FALSE";}

return var_dump($keywords_unique); // trim(stripslashes($keywords));

// use str_ireplace() for case insensitive find replace in php5.
// $keywords = str_replace($find, $replace, $keywords);

}

NOTE that I am returning var_dump for testing, normally i would simply return $keywords;.

coopster

8:27 pm on Jun 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you checked the array prior to running it through array_unique to see what is there?
$keywords_array = explode("\n", $keywords); 
print '<'pre>'; print_r($keywords_array); exit('</pre>';
$Keywords_unique = array_unique($keywords_array);

erikcw

10:58 pm on Jun 5, 2004 (gmt 0)

10+ Year Member



When I run the following through and print_r the array before running it through array_unique.

$_POST['keywords'] contains:
test1
test2
test3

The function creates

Array
(
[0] => test 1
[1] => test 2
[2] => test 3
[3] => test 1
[4] => test 2
[5] => test 3
)

when it is run the array_uniqe() it returns NULL - I have no idea what is going on with this thing.

erikcw

5:03 pm on Jun 6, 2004 (gmt 0)

10+ Year Member



Just a clarification var_dump() gives NULL - r_print() returns nothing.

erikcw

6:20 pm on Jun 6, 2004 (gmt 0)

10+ Year Member



After repeatedly playing arround with this function, I went back to the php manual, and rewrote the part dealing with the arrays from scratch. A low & behold it started working! No more NULL!

Now I'm just trying to get the regular expression to find and replace correctly. Right now it will only replace words that are on the first line (before \n). I think this has something to do with /\bSTRING\b/. Any regexp junkies out there who can tell me how to match a word in this list (but it has to be at least a whole word).
If I used str_replace() and someone enter "e" then it will replace all the "e"s (such a (E)rik). I need the search to require at least one word, but maybe several, all on one line...


$find = strtolower($_POST['find']);
$replace = strtolower($_POST['replace']);
$keywords = trim(strtolower($keywords));

$PATTERNS = "/\b" . $find . "\b/";
$REPLACEMENTS = $replace;
$keywords .= "\n" . preg_replace($PATTERNS, $REPLACEMENTS, $keywords);

mykel79

10:20 pm on Jun 6, 2004 (gmt 0)

10+ Year Member



It seems like it's the
$keywords[5].[/5]=

that's messing everything up.
What is the "." there for exactly? It seems to work without it.

erikcw

11:11 pm on Jun 6, 2004 (gmt 0)

10+ Year Member



The '.' is to append the replaced version onto the old version. I made the following change and it started working:


$PATTERNS = "/\b" . $find . "\b/";
$REPLACEMENTS = $replace;
$temp .= preg_replace($PATTERNS, $REPLACEMENTS, $keywords);

I'm conducting testing right now, but it appears that everything works as expected.

Thanks for all of your help!