Forum Moderators: coopster

Message Too Old, No Replies

Find Replace with Regular Expressions

Need some help getting this regular expression up and running.

         

erikcw

12:51 am on May 28, 2004 (gmt 0)

10+ Year Member



Hi All,

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!

jamesa

9:32 am on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Regarding the first one:
>> $find = "/^" . $_POST['find'] . "$/";

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.

erikcw

8:16 pm on May 29, 2004 (gmt 0)

10+ Year Member



Thanks jamesa,

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?

coopster

3:55 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you saying that you want to qualify the "search" word as a word boundary...meaning that it must begin and end as specified, with spaces on each side of the word?

erikcw

4:16 pm on Jun 1, 2004 (gmt 0)

10+ Year Member



Sort of, the user enters a list of items in a textarea, and then has two text boxes, one for find, one for replace. I'd like to limit the search to exactly what is in the find box on a per line bases.

Any ideas?

coopster

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

WebmasterWorld Administrator 10+ Year Member



The reason str_replace [php.net] probably won't work is that it seems you will need some regular expression matching. So, rather than saying it is "sloppy", actually it is that it is too accurate.

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].

erikcw

8:41 pm on Jun 1, 2004 (gmt 0)

10+ Year Member



This is what it is supposed to do:

OLD LIST... (list is acctually a variable that will be printed back to a textarea)
erik johnson
rob washington
howie smith

COMMAND... (findappend();)
find erik, append eric

NEW LIST...
erik johnson
rob washington
howie smith
eric johnson

erikcw

1:38 am on Jun 3, 2004 (gmt 0)

10+ Year Member



Is there a match slightly broader than \b? (\Z?) I want to not only match an entire word, but everything until the next \n.

WhosAWhata

2:13 am on Jun 3, 2004 (gmt 0)

10+ Year Member



you could try something like

// 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