Skip to main content
Known Participant
March 6, 2009
Question

Help with simple Replace?

  • March 6, 2009
  • 3 replies
  • 411 views
Hi,

Ok, I've been staring at this for way too long. I know I'm missing something dumb here but I've been trying for over an hour and I just can't find it! Please take a look...

I'm looping through a series of form variables, inserting them to a table. All working just fine. But on the "cell number" value, I'm trying to strip out dashes if anyone inserts them. IE, "123-456-7890" should become "1234567890".

Here's what I've got...

<cfset cellnumber = "FORM.cellnumber_#Pcount#">
<cfset cellnumber = #Replace(cellnumber,"-","","all")# >
<cfset cellnumber = Evaluate(cellnumber)>

And then a standard cfquery insert of #cellnumber# among other variables.

All the inserts work fine. They just all still have dashes in them. I've tried every variation of syntax I can think of. Can anyone spot the problem? Is there some problem with the logical flow I'm not seeing?

Thanks...
    This topic has been closed for replies.

    3 replies

    Known Participant
    March 7, 2009
    Ah, makes sense, thanks.

    I actually spent like 45 minutes making an ReReplace statement that would strip out all the characters I could think of that someone might put in a number. It strips dashes, dots, spaces, parentheses etc... then I realized I should have done like you said and just taken out all non-numerics. Oh well, it works good now... Thanks!
    Inspiring
    March 6, 2009
    because how you had it before, you were trying to replace dashes in a
    string "FORM.cellnumber_XXX" (XXX = value of your RCount variable), not
    in the value of that form variable.

    you can actually drop the whole Evaluate() thing and use one line of
    code instead of 3:

    <cfset cellnumber = Replace(Form['cellnumber_' & Pcount],"-","","all")>

    some people tend to insert spaces instead of dashes in the phone number
    fiields, or even some other characters...
    if you want to strip out all non-numeric characters from your string
    then use ReReplace() instead:

    <cfset cellnumber = ReReplace(Form['cellnumber_' & Pcount],"\D","","all")>


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Known Participant
    March 6, 2009
    Ok, fixed it but I can't say I understand why. All I did was swap the second and third cfset. So for instance, this doesn't work:

    <cfset cellnumber = "FORM.cellnumber_#Pcount#">
    <cfset cellnumber = #Replace(cellnumber,"-","","all")# >
    <cfset cellnumber = Evaluate(cellnumber)>

    BUT this does:

    <cfset cellnumber = "FORM.cellnumber_#Pcount#">
    <cfset cellnumber = Evaluate(cellnumber)>
    <cfset cellnumber = #Replace(cellnumber,"-","","all")# >

    So for some reason the Evaluate has to come first. I can't figure out why it matters. Can anyone explain???

    Thanks...