|
|
|
#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 |
|
![]() |
| Outils de la discussion | |
|
|