|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 <span class="replacePart">... new text here ....</span>. How could I go about this? Could I use a regular expression? Thank you Nicolaas |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
..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 <span class="replacePart">... new text here ....</span>. > >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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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? <?php $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" ?> |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
> why this is so complex, why not just use a simple srt_replace
that's str_replace |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
windandwaves <nfrancken@gmail.com> 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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
..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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On Nov 7, 10:21 am, Michael Fesser <neti...@gmx.de> 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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On Nov 7, 9:55 am, Good Man <he...@letsgo.com> wrote:
> windandwaves <nfranc...@gmail.com> 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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On Nov 8, 11:13 pm, windandwaves <nfranc...@gmail.com> wrote:
> On Nov 7, 10:21 am, Michael Fesser <neti...@gmx.de> 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 |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
On Nov 9, 1:06 pm, Darko <darko.maksimo...@gmail.com> wrote:
> On Nov 8, 11:13 pm, windandwaves <nfranc...@gmail.com> wrote: > > > > > On Nov 7, 10:21 am, Michael Fesser <neti...@gmx.de> 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 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
On Nov 10, 12:33 am, windandwaves <nfranc...@gmail.com> wrote:
> On Nov 9, 1:06 pm, Darko <darko.maksimo...@gmail.com> wrote: > > > > > On Nov 8, 11:13 pm, windandwaves <nfranc...@gmail.com> wrote: > > > > On Nov 7, 10:21 am, Michael Fesser <neti...@gmx.de> 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 asthey provided us with such function ![]() |
|
![]() |
| Outils de la discussion | |
|
|