2

Possible Duplicate:
Preg Replace - replace second occurance of a match

I have a string that includes the word rules twice. I need to find and replace the 2nd word. Tried fooling around with str_replace() but couldn't get anything, the 4th parameter wasn't what I expected.

Here is an example string:

http://localhost/proj1/modstart/admin/index.php?i=rules&sid=397ab1f6b8eb8a17787438a7e2e60ea3&mode=rules

After my replace it should look like this:

http://localhost/proj1/modstart/admin/index.php?i=rules&sid=397ab1f6b8eb8a17787438a7e2e60ea3&mode=manage

I read that preg_replace() could help, but I don't know how to write patterns.

Ideas?

P.S: Don't suggest splitting the string into two variables, that wouldn't serve my needs.

Community
  • 1
  • 1
aborted
  • 4,481
  • 14
  • 69
  • 132
  • Though you could also just write a simple regex matching the word twice, keeping the first part, filler and remainder. * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Oct 06 '12 at 20:17
  • It's not a dupe. My question differs from the one you linked, not that much but it does. – aborted Oct 06 '12 at 20:23
  • is the second keyword always going to be the end of string?? – Rajat Singhal Oct 06 '12 at 20:26
  • Nope, it wont. :\ I'd be using substr_replace() in that case. – aborted Oct 06 '12 at 20:27

3 Answers3

0

To not use a regular expression, and also to not store anything into a second variable, you could use str_replace() with a little magic from strpos():

$string = substr($string, 0, strpos($str, 'rules') + 5) . str_replace('rules', 'whatever', substr($string, strpos($string, 'rules') + 5));

This will take the full string up-to the end of the first instance of rules and then do the string-replacement on the second-part of the string which will contain any other instance of the word.

The same thing, but a little more cleaner (yes, by using a second variable):

$pos = strpos($string, 'rules') + 5;
$string = substr($string, 0, $pos) . str_replace('rules', 'whatever', substr($string, $pos));

If the word to find+replace is dynamic or you want to use a different word on different pages, you could make that a variable, like this:

$findMe = 'rules';
$replaceWith = 'whatever';
$pos = strpos($string, $findMe) + strlen($findMe);
$string = substr($string, 0, $pos) . str_replace($findMe, $replaceWith, substr($string, $pos));
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
0

It would be a very good idea to learn about regular expressions. In PHP, you can accomplish your find/replace like this:

$result = preg_replace('/(rules.*?)rules/','$1manage',$str,1);

It basically finds "rules" once, then anything, then rules a second time, then puts it all back before the second match and replaces the word.

jimp
  • 16,999
  • 3
  • 27
  • 36
0

You should use regex >>

$new = preg_replace('/\brules\b(?!.*\brules\b)/', 'manage', $old); 

It is a good idea to use word boundaries \b, so it will not match some larger strings that contain "rules", such as "preudorules".

Negative lookahead (?!.*\brules\b) ensures there is no other word "rules" behind, so the one you are replacing is the last one.

Ωmega
  • 42,614
  • 34
  • 134
  • 203