Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP on find and replace.....

Status
Not open for further replies.

Bramvg

IS-IT--Management
Jan 16, 2001
135
BE
Hi,

I'm dealing with following problem:

In my original text I want to replace all the signs: '<tag>' with each time a difference value.

I give an example sentence:
'Yesterday I <tag> to the shop and <tag> myself some cd's'

I want to replace the first <tag> with the value 'WENT' and the second <tag> with the value 'BOUGHT'. [the options WENT and BOUGHT come out of a database and have unique names on which I can check.. (first/second/....)]

So far I can only replace the first one like this:

#REReplace(&quot;#list#&quot;, &quot;<tag>&quot;, &quot;#query.first#&quot;, &quot;one&quot;)#

The 'problem' with the REReplace is that I cannot check the 'position' of the different <tag> in a sentence. I cannot say: replace the first <tag> with query.first and when you find the second one replace it with query.second


ANY HELP MORE THAN WELCOME ;-)
Many thankx
bram





 
There's all sorts of ways to do this. To recommend the best possbiel solution, could you answer a few questions?[ol][li]Will you always be searching for the string '<tag>' specifically, or will you be looking for all sorts of tags like '<b>' or '<i>' etc.?[/li][li]Is there a set amount of '<tag>' to be replaced in a string, or could it vary?[/li][/ol] - tleish
 
REReplace - by definition returns string with a regular expression replaced with substring in the specified scope. This is a case-sensitive search.

Syntax
REReplace(string, reg_expression, substring [, scope ])

is there a special reason why you want to use this function here?

can you use this to directly output query fields in the right places without replace function?

'Yesterday I #query.first#
to the shop and #query.second#
myself some cd's'

Sylvano
dsylvano@hotmail.com

&quot;every and each day when I learn something new is a small victory...&quot;
 
Hi,

I could dot this: yesterday I #query.first#.... but the thing is: the question itself comes out of a database.

so it's like:

<cfoutput query=&quot;name&quot;>
#question#
</cfoutput>

Where the question itself contains the <tag> tags...


Any help, more than welcome :)
bram


 
if I understand you correctly, you are getting database field (question) that looks like this and you want first <tag> to be replaced with #query.first#, and second <tag> to be replaced with #query.second#

<cfset dbField=&quot;
Yesterday I <tag> to the shop and <tag> myself some cd's&quot;>

you can use Replace function to replace <tag> one by one:

<cfset newField=Replace(dbField, &quot;<tag>&quot;, &quot;went&quot;)>
<cfset question=Replace(newField, &quot;<tag>&quot;, &quot;bought&quot;)>

<CFOUTPUT>#question#</CFOUTPUT>


that is because if scope in the replace function is not specified, the default in one

scope defines how to complete the replace operation:

ONE -- Replace only the first occurrence (default).
ALL -- Replace all occurrences.


Sylvano
dsylvano@hotmail.com

&quot;every and each day when I learn something new is a small victory...&quot;
 
Sylvano,

That did the trick !!! Thank you very much.
I was looking for a too difficult sollution [I tried CFLOOPS/.......]

Thankx!
bram
 
you can use this loop to find all occurances of string you are looking for in the database field:

<cfset dbField=&quot;Yesterday I <tag> to the shop and <tag> myself some cd's&quot;>
<cfset replacements=&quot;went,bought&quot;>

<cfset tagsPos = 0>
<cfset repPos = 0 >

<cfset allTagsPos=&quot;&quot;>
<cfloop index=&quot;tagCount&quot; list=&quot;#dbField#&quot; delimiters=&quot; &quot;>
<cfset tagsPos = tagsPos + 1>
<cfif tagCount EQ &quot;<tag>&quot;>
<cfset repPos = repPos + 1>
<cfset newTag = ListGetAt(replacements, repPos)>
<cfset dbField = Replace(dbField, &quot;<tag>&quot;, newTag)>
</cfif>
</cfloop>

this loop will replace both <tag>'s, with went and bought, respectively
be careful with this loop; in case such as this, you have to know how many &quot;<tag>&quot;'s you have in db field, and exact order so you can populate replacement list in the order you want <tag>'s to be raplaced; if you don't do that, cf will return error
it will do whant you want it to do, but I am not convinced, that this is the best way;
maybe you want to do some more research on this; you might be able to find better solutions Sylvano
dsylvano@hotmail.com

&quot;every and each day when I learn something new is a small victory...&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top