473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Nov 4 '07 #1
11 1903
..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
Nov 4 '07 #2
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"
?>

Nov 4 '07 #3
why this is so complex, why not just use a simple srt_replace

that's str_replace

Nov 4 '07 #4
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

Nov 6 '07 #5
windandwaves <nf*******@gmail.comwrote 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
Nov 6 '07 #6
..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
Nov 6 '07 #7
On Nov 7, 10:21 am, Michael Fesser <neti...@gmx.dewrote:
.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

Nov 8 '07 #8
On Nov 7, 9:55 am, Good Man <he...@letsgo.comwrote:
windandwaves <nfranc...@gmail.comwrote 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

Nov 8 '07 #9
On Nov 8, 11:13 pm, windandwaves <nfranc...@gmail.comwrote:
On Nov 7, 10:21 am, Michael Fesser <neti...@gmx.dewrote:
.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

Nov 9 '07 #10
On Nov 9, 1:06 pm, Darko <darko.maksimo...@gmail.comwrote:
On Nov 8, 11:13 pm, windandwaves <nfranc...@gmail.comwrote:
On Nov 7, 10:21 am, Michael Fesser <neti...@gmx.dewrote:
.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

Nov 9 '07 #11
On Nov 10, 12:33 am, windandwaves <nfranc...@gmail.comwrote:
On Nov 9, 1:06 pm, Darko <darko.maksimo...@gmail.comwrote:
On Nov 8, 11:13 pm, windandwaves <nfranc...@gmail.comwrote:
On Nov 7, 10:21 am, Michael Fesser <neti...@gmx.dewrote:
.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 :)

Nov 10 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions...
6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
4
by: joebob | last post by:
The following script if run in Internet Explorer should display a thumbview of a webpage that you point it to. To test it, replace test.htm with a valid html file. The problem I'm having is that...
25
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
8
by: pras.vaidya | last post by:
Hi , below given question was asked to me during an interview and i figured it out little tricky . It would be a great help if anyone could solve it. Code : - main() { char...
6
by: localhost | last post by:
I have a string that looks like this: "document.form1.textBox1.focus ();document.form1.textBox1.select();" I want to replace the text between "document.form1." and ".focus()", as well as...
2
by: pruebauno | last post by:
I am currently working on a tricky problem at work. I googled around a bit, but "time intervals" did not come up with anything useful. Although I have some rough idea of how I could solve it, I...
9
by: howachen | last post by:
Hi, I have one very simple tricky question which is quite interesting, I would like to share with all of you here... //======================================= <script...
10
by: goacross | last post by:
i found something tricky this morning. char *p="abc"; 1. char m=1;// m='b' 2. char n=sizeof('h'); // n=1; I guess the reason of 1is,
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.