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, 10h21   #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, 10h44   #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, 15h21   #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, 16h01   #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, 20h45   #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, 20h55   #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, 21h21   #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, 22h13   #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
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 12h50.


É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,17163 seconds with 16 queries