Forum Moderators: coopster
I'm trying to create a function to perform find and replace on data entered into a form textarea. I tried using str_replace() - but it is a too sloppy for what I need. So I'm thinking regular expressions are the way to go.
There are two functions, one is a true find and replace, and the second is a "find and append" (basically finds a word in the string, and then appends another version onto the end of the string.).
I've played with it allot, unfortunatly I can't get it to work right.
function findreplace() {
extract($GLOBALS);$find = "/^" . $_POST['find'] . "$/";
$replace = $_POST['replace'];$PATTERNS = $find;
$REPLACEMENTS = $replace;
$keywords = preg_replace($PATTERNS, $REPLACEMENTS, $keywords);// $keywords = str_replace($find, $replace, $keywords);
return $keywords;
}
function findappend() {
extract($GLOBALS);$find2 = $_POST['find2'];
$replace2 = $_POST['replace2'];$PATTERNS = $find2;
$REPLACEMENTS = $replace2;
$keywords = preg_replace($PATTERNS, $REPLACEMENTS, $keywords);/*
$keywords = str_replace($find2, $replace2, $keywords);
*/// Append find to then end of the string
$keywords .= "\n" . $find2;return $keywords;
}
What am I missing?
Thanks!
Using the ^ and $ means that there can't be anything before or after $_POST['find']. So if $_POST['find'] = 'cat', and $keywords = 'cat' you have a match. If $keywords = 'turtle cat frog elephant' that's not a match because there's more than just 'cat' in that string. If you wanted the second example to match then remove the ^ and $.
Regarding the second one. I'm not exactly clear on what you're trying to do but my guess is that you'd want to replace this:
$keywords = preg_replace($PATTERNS, $REPLACEMENTS, $keywords);
with this:
$keywords2 = preg_replace($PATTERNS, $REPLACEMENTS, $keywords);
and this:
$keywords .= "\n" . $find2;
with this:
$keywords .= "\n" . $keywords2;
This way $keywords2 becomes the altered string and $keyword remains unaltered so when you combine them you have both versions.
I gave that a try but here is what happened:
for the first function, it simply turned the script into the standard vanilla php srt_replace().
For the second function it basically doubled the list.
Here are some more details on what I am trying to do:
users enter a list of data in a form textarea:
item1
item2
item3
$_POST['keywords'];
I want to run the find and replace on each line (\n).
So the function would ouput nothing if 'item' were inputed (becuase item does not exist) - however if item2 were put in then item2 would be replaced. So I need a semi exact match (but I guess I can't use /^ . $find . $/ cause it checks the whole list?
Any ideas?
I'm not sure I follow on the per line basis, but we can start with word boundaries to see if you get the results you expect.
$find = "/\b" . $_POST['find'] . "\b/";"\b" is a word boundary [php.net].
// separate them into an array by lines
$explode = explode("\n",your variable);
// loop through each array entry (line)
foreach($explode as $k => $v){
//search each line for the string
if(your find script, searching $v) {
// if found, add the value of that line to an array
$lines[] = $v;
}
}
now $lines is an array that contains all of the lines that contained your match