*_replace are too good!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

*_replace are too good!

Post by PnHoPob »

Literally... they're doing their job too well for me :P

Code: Select all

$string = "abcdeiou";

$string = str_replace("a", "Dear(", $string);
//Current string is "Dear(bcdeiou"

$string = str_replace("e", "Sally(", $string);
However, I just want to replace the second e [ Dear(bcdeiou ], and not the e in Dear.

So, I guess my question is: How do I str or preg replace only stuff that hasn't been replaced yet?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

$string  = 'abcdeiou';

$find = array('a', 'e');

$replace = array('Dear(', 'Sally(');

$string = str_replace($find, $replace, $string);

?>
:)
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Post by PnHoPob »

Well, that's what I've been doing and it seems to me that making an array works the same as doing it separately.
Am I wrong?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

aha, you are right, my apologies.

A string parser would be the solution then, that uses preg_match (or more likely preg_match_all) with the PREG_OFFSET_CAPTURE flag.. if I get time (which is likely) and I get bored (even more likely) later then I will give writing one a go.
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Post by PnHoPob »

cool, thanks!
Let me explain what I'm doing a little better.

I'm trying to convert a file that has both one and two characters long tokens.
First, I replace the two character long tokens so the one longs don't screw up the two longs.
Then, I replace the one character long ones.

However, let's say the character "o" is the "cat" token and the character "horse" is a two long token that has already been replaced.
When "o" is replaced by "cat", the "horse" that has already been replaced becomes "hcatrse".
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Well.... it can be done with regex too...

Code: Select all

$string = "Dear Peter...";
$new = preg_replace('/^(.*?e.*?)e(.*)$/s', '$1a$2', $string); //Dear Pater...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

That doesn't achieve what is asked for, that just replaces the 3rd occurance of e in a string.

The problem here is that the replacement value for the 1st search contains a matching string for the 2nd search, so the 1st replacement is broken with the 2nd replacement. (note the e in Dear( gets replaced with Sally( in the example)

I started on a function, but then my employers decided to drop a bomb on me, also known as a notification of redundancy, so I am scrambling for a new job atm :)
Post Reply