tricky replace function
am 04.11.2007 11:21:31 von WindAndWaves
Hi Folk
I need to write a tricky replacement function.
C = replace A with B in C
C = replace D with E in C
examples of A could be "a cat climbs a tree", examples of B could be
"a dog climbs a alligator". Thereby, I want to make sure that parts
that have already been replaced will not be replaced again (by the E
replaces D statement for example). I will mark all new text (Bs and
Es) with ... new text here .....
How could I go about this? Could I use a regular expression?
Thank you
Nicolaas
Re: tricky replace function
am 04.11.2007 11:44:38 von Michael Fesser
..oO(windandwaves)
>I need to write a tricky replacement function.
>
>C = replace A with B in C
>C = replace D with E in C
>
>examples of A could be "a cat climbs a tree", examples of B could be
>"a dog climbs a alligator". Thereby, I want to make sure that parts
>that have already been replaced will not be replaced again (by the E
>replaces D statement for example). I will mark all new text (Bs and
>Es) with ... new text here .....
>
>How could I go about this? Could I use a regular expression?
You could try strtr() first, called with only two parameters. See the
manual for details.
http://www.php.net/strtr
Micha
Re: tricky replace function
am 04.11.2007 16:21:17 von Macca
Unless I am misunderstanding what you are looking for, as I cant see
why this is so complex, why not just use a simple srt_replace
function?
$find = array('cat','tree');
$replace = array('dog','alligator');
$phrase = "a cat climbs a tree";
echo str_replace($find,$replace,$phrase);
//outputs "a dog climbs a alligator"
?>
Re: tricky replace function
am 04.11.2007 17:01:57 von Macca
> why this is so complex, why not just use a simple srt_replace
that's str_replace
Re: tricky replace function
am 06.11.2007 21:45:53 von WindAndWaves
Why is this so hard?
I can do a str_replace. That is easy. However, I do many of them AND
I want to make sure that one replacement does not override another...
e.g.
statement 1 could be: replace "cats" with "dogs"
statement 2 could be: replaced "do" with "did"
as you can see, this could turn "cats" into "didgs"
That is what is the hard part.
Thank you
Nicolaas
Re: tricky replace function
am 06.11.2007 21:55:15 von Good Man
windandwaves wrote in news:1194381953.358772.178700
@q3g2000prf.googlegroups.com:
> Why is this so hard?
>
> I can do a str_replace. That is easy. However, I do many of them AND
> I want to make sure that one replacement does not override another...
>
> e.g.
> statement 1 could be: replace "cats" with "dogs"
> statement 2 could be: replaced "do" with "did"
>
> as you can see, this could turn "cats" into "didgs"
>
> That is what is the hard part.
then yes, go for regex so you can specify beginnings/ends of words as
opposed to characters in a string
Re: tricky replace function
am 06.11.2007 22:21:56 von Michael Fesser
..oO(windandwaves)
>Why is this so hard?
>
>I can do a str_replace. That is easy. However, I do many of them AND
>I want to make sure that one replacement does not override another...
>
>e.g.
>statement 1 could be: replace "cats" with "dogs"
>statement 2 could be: replaced "do" with "did"
>
>as you can see, this could turn "cats" into "didgs"
>
>That is what is the hard part.
That's why I suggested to give strtr() a try, which should avoid this
problem.
Micha
Re: tricky replace function
am 08.11.2007 23:13:34 von WindAndWaves
On Nov 7, 10:21 am, Michael Fesser wrote:
> .oO(windandwaves)
>
> >Why is this so hard?
>
> >I can do a str_replace. That is easy. However, I do many of them AND
> >I want to make sure that one replacement does not override another...
>
> >e.g.
> >statement 1 could be: replace "cats" with "dogs"
> >statement 2 could be: replaced "do" with "did"
>
> >as you can see, this could turn "cats" into "didgs"
>
> >That is what is the hard part.
>
> That's why I suggested to give strtr() a try, which should avoid this
> problem.
>
> Micha
Hi Micha
>From what I understand strtr only replace characters, not sentences,
so I am not sure if that would work.
Cheers
Nicolaas
Re: tricky replace function
am 08.11.2007 23:14:36 von WindAndWaves
On Nov 7, 9:55 am, Good Man wrote:
> windandwaves wrote in news:1194381953.358772.178700
> @q3g2000prf.googlegroups.com:
>
> > Why is this so hard?
>
> > I can do a str_replace. That is easy. However, I do many of them AND
> > I want to make sure that one replacement does not override another...
>
> > e.g.
> > statement 1 could be: replace "cats" with "dogs"
> > statement 2 could be: replaced "do" with "did"
>
> > as you can see, this could turn "cats" into "didgs"
>
> > That is what is the hard part.
>
> then yes, go for regex so you can specify beginnings/ends of words as
> opposed to characters in a string
Hi
I thought that would be the way to go. I dont think I have enough
skills to write a fully-fledged regex for this. Would you have any
ideas?
Thank you
Nicolaas
Re: tricky replace function
am 09.11.2007 01:06:20 von darko
On Nov 8, 11:13 pm, windandwaves wrote:
> On Nov 7, 10:21 am, Michael Fesser wrote:
>
>
>
> > .oO(windandwaves)
>
> > >Why is this so hard?
>
> > >I can do a str_replace. That is easy. However, I do many of them AND
> > >I want to make sure that one replacement does not override another...
>
> > >e.g.
> > >statement 1 could be: replace "cats" with "dogs"
> > >statement 2 could be: replaced "do" with "did"
>
> > >as you can see, this could turn "cats" into "didgs"
>
> > >That is what is the hard part.
>
> > That's why I suggested to give strtr() a try, which should avoid this
> > problem.
>
> > Micha
>
> Hi Micha
>
> >From what I understand strtr only replace characters, not sentences,
>
> so I am not sure if that would work.
>
> Cheers
>
> Nicolaas
Please read the manual more carefully. Because, yes, the first few
lines say the following:
> This function returns a copy of str, translating all occurrences of each
> character in from to the corresponding character in to.
What also says there is the following:
> strtr() may be called with only two arguments. If called with two arguments it
> behaves in a new way: from then has to be an array that contains string -> string
> pairs that will be replaced in the source string.
> strtr() will always look for the longest possible match first and will *NOT* try
> to replace stuff that it has already worked on.
Cheers
Re: tricky replace function
am 10.11.2007 00:33:47 von WindAndWaves
On Nov 9, 1:06 pm, Darko wrote:
> On Nov 8, 11:13 pm, windandwaves wrote:
>
>
>
> > On Nov 7, 10:21 am, Michael Fesser wrote:
>
> > > .oO(windandwaves)
>
> > > >Why is this so hard?
>
> > > >I can do a str_replace. That is easy. However, I do many of them AND
> > > >I want to make sure that one replacement does not override another...
>
> > > >e.g.
> > > >statement 1 could be: replace "cats" with "dogs"
> > > >statement 2 could be: replaced "do" with "did"
>
> > > >as you can see, this could turn "cats" into "didgs"
>
> > > >That is what is the hard part.
>
> > > That's why I suggested to give strtr() a try, which should avoid this
> > > problem.
>
> > > Micha
>
> > Hi Micha
>
> > >From what I understand strtr only replace characters, not sentences,
>
> > so I am not sure if that would work.
>
> > Cheers
>
> > Nicolaas
>
> Please read the manual more carefully. Because, yes, the first few
> lines say the following:
>
> > This function returns a copy of str, translating all occurrences of each
> > character in from to the corresponding character in to.
>
> What also says there is the following:
>
> > strtr() may be called with only two arguments. If called with two arguments it
> > behaves in a new way: from then has to be an array that contains string -> string
> > pairs that will be replaced in the source string.
> > strtr() will always look for the longest possible match first and will *NOT* try
> > to replace stuff that it has already worked on.
>
> Cheers
Oh wow, my bad! Thank you.... I read that, but I did not really
understand it. Wow, that is great. What a joy!
Thank you - that does exactly what I need.
My apologies.....!
Thank you
Re: tricky replace function
am 10.11.2007 02:24:00 von darko
On Nov 10, 12:33 am, windandwaves wrote:
> On Nov 9, 1:06 pm, Darko wrote:
>
>
>
> > On Nov 8, 11:13 pm, windandwaves wrote:
>
> > > On Nov 7, 10:21 am, Michael Fesser wrote:
>
> > > > .oO(windandwaves)
>
> > > > >Why is this so hard?
>
> > > > >I can do a str_replace. That is easy. However, I do many of them AND
> > > > >I want to make sure that one replacement does not override another...
>
> > > > >e.g.
> > > > >statement 1 could be: replace "cats" with "dogs"
> > > > >statement 2 could be: replaced "do" with "did"
>
> > > > >as you can see, this could turn "cats" into "didgs"
>
> > > > >That is what is the hard part.
>
> > > > That's why I suggested to give strtr() a try, which should avoid this
> > > > problem.
>
> > > > Micha
>
> > > Hi Micha
>
> > > >From what I understand strtr only replace characters, not sentences,
>
> > > so I am not sure if that would work.
>
> > > Cheers
>
> > > Nicolaas
>
> > Please read the manual more carefully. Because, yes, the first few
> > lines say the following:
>
> > > This function returns a copy of str, translating all occurrences of each
> > > character in from to the corresponding character in to.
>
> > What also says there is the following:
>
> > > strtr() may be called with only two arguments. If called with two arguments it
> > > behaves in a new way: from then has to be an array that contains string -> string
> > > pairs that will be replaced in the source string.
> > > strtr() will always look for the longest possible match first and will *NOT* try
> > > to replace stuff that it has already worked on.
>
> > Cheers
>
> Oh wow, my bad! Thank you.... I read that, but I did not really
> understand it. Wow, that is great. What a joy!
>
> Thank you - that does exactly what I need.
>
> My apologies.....!
>
> Thank you
You should feel good, because it means it wasn't trivial :) as soon as
they provided us
with such function :)