Forum Moderators: coopster
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;.
$_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.
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);
$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!