You can nest your Replace() statement like so:
<cfset addresses = #form.addresses#>
<cfset addresses = Replace(Replace(addresses,
Main Topics
Browse All TopicsI have set up a textarea field for users to enter email addresses to send a notice to. I've been trying to convert the results into a comma-delimited list, which I can then use as a recipient list for CFMAIL. I've tried using the Replace and ChangeList Delims functions but nothing is working -- if I enter this in the text area:
1
2
3
4
5
I always get this as the result:
1 2 3 4 5
I need this:
1,2,3,4,5
Here's the form I'm using as a test:
<form action="#cgi.query_string#
<textarea name="addresses" cols="40" rows="10" wrap="physical"></textarea
<input name="Submit" type="submit" value="Submit">
</form>
I've tried all variations on the wrap attribute in the form.
Here's the code I've tried to change the results:
<cfif isDefined("form.addresses"
<cfset addresses=#form.addresses#
<cfset addresses = ListChangeDelims(addresses
<cfoutput>#addresses#</cfo
</cfif>
I've also tried this:
<cfif isDefined("form.addresses"
<cfset addresses = #form.addresses#>
<cfset addresses = Replace(addresses, "chr(10)&chr(13)", ",", "ALL")>
<cfoutput>addresses2</cfou
</cfif>
And this:
<cfif isDefined("form.addresses"
<cfset addresses = #form.addresses#>
<cfset addresses = Replace(addresses, "#chr(10)##(13)#", ",", "ALL")>
<cfoutput>addresses2</cfou
</cfif>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I have a search form that I got out of a book, where every word on the same line is 'and-ed', and different lines are 'or-ed' together. This works from a textarea, and easily distinguishes the different lines. This could easily be changed to create a comma seperated list... in fact, the data from the textarea is treated as a list by this function, with each line break being a delimeter.
<CFPARAM name="Search" default="">
<CFSET CRLF="#Chr(13)##Chr(10)#">
<CFIF #Search# EQ ""><CFSET SearchCriteriaSQL="">
<CFELSE>
<CFSET SearchCriteriaSQL=" and (0=1">
<CFLOOP INDEX="line" LIST="#Search#" Delimiters="#CRLF#">
<CFSET SearchCriteriaSQL = SearchCriteriaSQL & " OR (0=0">
<CFLOOP INDEX="word" LIST=#line# Delimiters=" ">
<CFSET SearchCriteriaSQL = SearchCriteriaSQL &
" AND #criteria# like '%" & Replace(word,"'","''","ALL
</CFLOOP>
<CFSET SearchCriteriaSQL = SearchCriteriaSQL & ")">
</CFLOOP>
<CFSET SearchCriteriaSQL = SearchCriteriaSQL & ")">
</CFIF>
Since all you want is to convert the values on each line to a list, you can do this:
<CFSET addresses= listChangeDelims(addresses
In fact, I tested it with a form here is the code on the action form:
--------------------------
Addresses is:<br>
<cfoutput>#adresses#</cfou
<br>Converted Addresses is:<br>
<CFSET addresses= listChangeDelims(addresses
<cfoutput>#addresses#</cfo
--------------------------
The results of the above is:
Addresses is:
EMAIL1@COMPANY.COM EMAIL2@MYADDR.NET ANOTHER@EMAIL.COM YAHOO@YIPPEE.WORKS
Converted Addresses is:
EMAIL1@COMPANY.COM,EMAIL2@
Note that in the listarea, they were on different lines, but when output here, it doesn't show that because of the nature of CR and LF when displayed on a Web Page.
Brucecrabtree,
pmascari's "Replace()" solution near the top is correct.
Alternativly if you wanted to use a regular expression like dgrafx, the proper way to do it is like below:
<cfif isDefined("form.addresses"
<cfset addresses = REReplace(FORM.addresses,"
<cfoutput>#addresses#</cfo
</cfif>
When i tried your solution dgrafx, I was getting multiple commas; my version would not.
Hi Guys,
>> js vaughan: You may be right (?) about the multiple commas as I didn't test.
Now that I think about it though, the multiple commas you may have gotten (I'm assuming from multiple cr's together) shouldn't be a problem as it doesn't change listlen - blank slot ya know. So you can't say that extra commas are a problem.
>>Bruce: It looks like all these simple solutions would work for you. They accomplish the same thing in slightly different ways.
So use rereplace or the listchangedelims that RCorfman posted above.
I guess I felt like my solution got buried in with the others. It was closes to what was being tried initiailly and I would note is one simple call, not multiple runs across the data like the nexted replace, and the processing is simpler than a regex processing would be. It seems like all three solutions would work though.
Bruce,
You weren't actually all that far off the right solution in the first place. The only error you made was to get the Carriage Return (chr(13)) and the Line Feed (chr(10)) the wrong way around.
RCorfman's solution is the most efficient solution, most readable.
Although, to be a pedant, the variables need scoping, you should be using StructKeyExists() rather than isDefined(), plus, personally, I would skip the #'s around the CRLF, which would give you :
<cfset variables.addresses="">
<cfif StructKeyExists(form,"addr
<CFSET variables.addresses= listChangeDelims(form.addr
<cfoutput>#variables.addre
</cfif>
Don't be giving me any points for this. RCorfman is the one who gave you the correct solution first.
Thanks everyone. I've gotten sidetracked on another project and haven't had a chance to try these different solutions yet. But I should probably close this question now, and based on everyone's comments here it looks like I should split the points between RCorfman and pmascari, so that's what I'll do. Thanks again.
Mr_Nil, like everyone, I'm still learning myself (a road that never ends). I'm curious as to why we shouldn't use isDefined()? I agree with dropping the overused #... I had actually gotten that part of the code from a book...
I would note that the nice thing about changeDelims in this case is it doesn't matter what order the chr(10) and chr(13) are in as either one is considered a delimeter (if I understand correctly).
> Mr_Nil, like everyone, I'm still learning myself (a road that never ends).
>
Tell me about it - 9 years now and I'm still learning new stuff.
> I'm curious as to why we shouldn't use isDefined()?
>
Its not so much that you shouldn't use it, but StructKeyExists is much more direct and efficient than isDefined().
In CFMX all variable scopes (apart from cookies - I think) are structures and always exist at the appropriate times eg. attributes scope struct exists when you call a custom tag or use cfmodule, arguments when you call a function and session etc when you have started a session using cfapplication.
When you use isDefined("myVariable"), or even isDefined("variables.myVar
Using StructKeyExists(variables,
> I would note that the nice thing about changeDelims in this case is it doesn't matter what order the chr(10) and chr(13) are
> in as either one is considered a delimeter (if I understand correctly).
>
Yeah I think you are right - its more than one character so it should treat them seperately. I was commenting more on the standard replace() function call that bruce had originally written. Switch the LF and CR around in the replace to be CRLF and the second example of replace() code would have worked. eg. Replace(addresses, "#chr(13)##chr(10)#", ",", "ALL")
Business Accounts
Answer for Membership
by: brucecrabtreePosted on 2006-03-17 at 17:53:14ID: 16222171
I found a javascript solution to this problem on the web here: http://jennifermadden.com/ javascript /stringEsc ape.html
I'd still like to know how to do it with CF, though, if anyone has a suggestion.