Author |
Topic |
|
RaiderUK
Average Member
United Kingdom
577 Posts |
Posted - 21 April 2001 : 21:19:56
|
was just building a quick word filter but its annoying me now, it will only replace the last word. can anyone help?
<% Dim strString, strBadWords, strNew, i, strWrd strString = "This is a bad word: worda and this: wordd and wordc" strBadWords = "worda|wordb|wordc|wordd" strBadWords = split(strBadWords, "|") for each i in strBadWords strWrd = cStr(i) strNew = Replace(strString, strWrd, "****") strNew = cStr(strNew) next %>
|
|
Marino
Starting Member
Canary Islands
42 Posts |
Posted - 22 April 2001 : 00:26:47
|
quote:
was just building a quick word filter but its annoying me now, it will only replace the last word. can anyone help?
<% Dim strString, strBadWords, strNew, i, strWrd strString = "This is a bad word: worda and this: wordd and wordc" strBadWords = "worda|wordb|wordc|wordd" strBadWords = split(strBadWords, "|") strNew = cStr(strString) for each i in strBadWords strWrd = cStr(i) strNew = Replace(strNew, strWrd, "****") strNew = cStr(strNew) next %>
Marino
|
|
|
RaiderUK
Average Member
United Kingdom
577 Posts |
Posted - 22 April 2001 : 07:28:47
|
i have tried it like that but still the same problem, any ideas?
|
|
|
HuwR
Forum Admin
United Kingdom
20584 Posts |
Posted - 22 April 2001 : 07:44:34
|
try this
for i = 0 to ubound(strBadWords ) fString = Replace(fString, strBadWords (i), string(len(strBadWords(i)),"*"), 1,-1,1) next
|
|
|
RaiderUK
Average Member
United Kingdom
577 Posts |
Posted - 22 April 2001 : 09:22:25
|
thanks alot, works brilliant now.
why wouldnt it work before? it looked right.
|
|
|
Marino
Starting Member
Canary Islands
42 Posts |
Posted - 23 April 2001 : 10:11:32
|
The code I wrote works for me, in every iteration strNew has:
This is a bad word: **** and this: wordd and wordc This is a bad word: **** and this: wordd and wordc This is a bad word: **** and this: wordd and **** This is a bad word: **** and this: **** and ****
The code in your post had the problem that you are checking for bad words in strString, while doing changes in strNew, so in first iteration you have in strNew the content of strString with first bad word changed and strString unchanged. In second iteration, you change the second bad word in strString, but strString was unchanged, so it still had the first bad word (well, actually in second iteration there are no changes, as there aren't "wordb" bad words in strString" ). and so on.
So at the end, you finish with strNew having StrString changed only the last iteration.
Iteration 1 (worda) strString = "This is a bad word: worda and this: wordd and wordc" strNew = "This is a bad word: **** and this: wordd and wordc" Iteration 2 (wordb) strString = "This is a bad word: worda and this: wordd and wordc" strNew = "This is a bad word: worda and this: wordd and wordc" Iteration 3 (wordc) strString = "This is a bad word: worda and this: wordd and wordc" strNew = "This is a bad word: worda and this: wordd and ****" Iteration 4 (wordd) strString = "This is a bad word: worda and this: wordd and wordc" strNew = "This is a bad word: worda and this: **** and wordc"
Marino
Edited by - marino on 23 April 2001 10:14:47 |
|
|
|
Topic |
|
|
|