Skip to main content
June 26, 2010
Answered

How do I display variables with # in them?

  • June 26, 2010
  • 2 replies
  • 1516 views

Hello Cf peeps.

I let users of my site create unique usernames and upload pictures.  They are then able to search those items for the exact name they entered.

Today a user signed up with the name goboy#4.

How can I avoid getting a cf error when on his profile page I output <cfoutput>#username#</cfoutput> where username = goboy#4?

Thanks!

    This topic has been closed for replies.
    Correct answer jjsand28

    You have to escape them by adding and additional # but since it is the middle of a variable may have to write some kind of filter. For future accounts I would create a script that does not allow special characters before the account is created.

    2 replies

    ilssac
    Inspiring
    June 26, 2010

    idesdema wrote:

    <cfoutput>#username#</cfoutput> where username = goboy#4?

    Thanks!

    That should not have caused an error.  There are places where having a reserved character in the stirng would matter.  But that simple output statement should not be one of them.

    June 28, 2010

    Good point Ian.  The one place it is happening is in the name of images.  People can upload pictures and

    when outputting the image path, if the name of the picture has a # in it, the image won't display.

    However, the solution we reached does solve that problem.

    Inspiring
    June 28, 2010

    Are you actually getting a CF error?  Can you show us the message, and the offending code?  From what you say in your latest post, the problem is not from CF, but that you have a hash mark in the URL for the image, which can cause problems when the browser tries to do a GET to that address to retrieve the image data.

    # marks inside of a CF variable usually only cause problems if you try to do an evaluate of an expression containing that variable.  Outputting the variable's contents should not cause a problem.

    -reed

    jjsand28Correct answer
    Inspiring
    June 26, 2010

    You have to escape them by adding and additional # but since it is the middle of a variable may have to write some kind of filter. For future accounts I would create a script that does not allow special characters before the account is created.

    June 26, 2010

    Ok so I'll want to search the var for a # character, if yes, set new_user = Replace("#username#","#","##","a'll")

    Or something like that.

    Thanks!

    Inspiring
    June 26, 2010

    That should work.