Forum Moderators: coopster

Message Too Old, No Replies

Is there an exact match str_replace()?

         

erikcw

4:48 am on May 26, 2004 (gmt 0)

10+ Year Member



Is there a way to access an "exact match" find replace function?
Below is the code I've been playing with. However it is proding the following results:

$find=dog;
$replace=dogg;

OUTPUT doggg.

If I were to tell it to:
$find=e;
$replace=erik;
$words="easy does it"

OUTPUT "erikasy doeriks it"


function findreplace() {
extract($GLOBALS);

$find = $_POST['find'];
$replace = $_POST['replace'];

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

return $words;
}

Should I be using a different function?

Thanks!

stuartc1

11:09 am on May 26, 2004 (gmt 0)

10+ Year Member



Hi,

Hmmm.. it depends on what you mean by 'exact match'. From what you have it is actually an exact match, but it lacks conditions. For example if you want to replace a instances of 'e' but the condition is it must be the first or last character of the string - then you need something more powerful like regular expressions. There are a couple of functions for this, check out [php.net...] for starters (it has other functions listed on this page also).