Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion replacelist reorders returned string

I'm trying to clean up submitted file names.

I'm using replacelist(filename,"',##,&, ",",,and,-") which should

  • remove ' and #
  • replace & with and
  • replace spaces with -.

When given "apost's & pound#.JPG", instead of returning:

    "aposts-and-pound.jpg" 

replaceList returns:

    "apostandspound-.JPG"

I'm using ColdFusion 10.

like image 628
Ookamikun Avatar asked Jan 24 '13 17:01

Ookamikun


1 Answers

This is not reordering - it is due to how the CF list processing works - empty elements are ignored/removed.

Some of the List~ string processing functions have an additional argument to change this behaviour (i.e. treat empty elements as an empty string), but ReplaceList doesn't appear to.

You can solve this by doing the removals in a separate step to the replacements:

<cfset NewFilename = rereplace(Filename,"['##]","","all") />
<cfset NewFilename = replacelist(NewFilename,"&, ","and,-") />

or

replacelist( rereplace(filename,"['##]","","all") , "&, " , "and,-" )
like image 194
Peter Boughton Avatar answered Oct 30 '22 13:10

Peter Boughton