PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > tricky replace function
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
tricky replace function

Réponse
 
LinkBack Outils de la discussion
Vieux 04/11/2007, 11h21   #1
windandwaves
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut tricky replace function

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

  Réponse avec citation
Vieux 04/11/2007, 11h44   #2
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

..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
  Réponse avec citation
Vieux 04/11/2007, 16h21   #3
macca
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

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"
?>

  Réponse avec citation
Vieux 04/11/2007, 17h01   #4
macca
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

> why this is so complex, why not just use a simple srt_replace

that's str_replace

  Réponse avec citation
Vieux 06/11/2007, 21h45   #5
windandwaves
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

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

  Réponse avec citation
Vieux 06/11/2007, 21h55   #6
Good Man
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

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
  Réponse avec citation
Vieux 06/11/2007, 22h21   #7
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

..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
  Réponse avec citation
Vieux 08/11/2007, 23h13   #8
windandwaves
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

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

  Réponse avec citation
Vieux 08/11/2007, 23h14   #9
windandwaves
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

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

  Réponse avec citation
Vieux 09/11/2007, 01h06   #10
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

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

  Réponse avec citation
Vieux 10/11/2007, 00h33   #11
windandwaves
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

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

  Réponse avec citation
Vieux 10/11/2007, 02h24   #12
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: tricky replace function

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 as
they provided us
with such function

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17915 seconds with 20 queries